Skip to content

Commit

Permalink
Customize name of CUP generated class containing the names of termina…
Browse files Browse the repository at this point in the history
…l tokens

- Make CUP output the symbol constant code as an interface rather than as a class.
- The default name of the CUP generated class/interface containing the names of terminal tokens is "sym". Use the custom name "SymbolConstants" instead.
- Make the generated lexer class implement the the interface SymbolConstants.
  • Loading branch information
romildo committed Oct 7, 2016
1 parent e047300 commit e46e955
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
</goals>
</execution>
</executions>
<configuration>
<symbolsName>SymbolConstants</symbolsName>
<symbolsInterface>true</symbolsInterface>
</configuration>
</plugin>

<plugin>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/main/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.beust.jcommander.ParameterException;
import java_cup.runtime.Symbol;
import parse.Lexer;
import parse.sym;
import parse.SymbolConstants;

// command line options
class DriverOptions {
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void lexicalAnalysis(Reader input) throws IOException {
do {
tok = lexer.next_token();
System.out.println(tok);
} while (tok.sym != sym.EOF);
} while (tok.sym != SymbolConstants.EOF);
}

}
16 changes: 9 additions & 7 deletions src/main/jflex/lexer.jflex
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import java_cup.runtime.Symbol;
%public
%final
%class Lexer
%implements SymbolConstants
%cupsym SymbolConstants
%cup

%%

[ \t\f\n\r]+ { /* skip */ }

[0-9]+ ("." [0-9]+)? { return new Symbol(sym.LITINT, yytext()); }
[0-9]+ ("." [0-9]+)? { return new Symbol(LITINT, yytext()); }

"+" { return new Symbol(sym.PLUS); }
"-" { return new Symbol(sym.MINUS); }
"*" { return new Symbol(sym.TIMES); }
"/" { return new Symbol(sym.DIV); }
"(" { return new Symbol(sym.LPAREN); }
")" { return new Symbol(sym.RPAREN); }
"+" { return new Symbol(PLUS); }
"-" { return new Symbol(MINUS); }
"*" { return new Symbol(TIMES); }
"/" { return new Symbol(DIV); }
"(" { return new Symbol(LPAREN); }
")" { return new Symbol(RPAREN); }

. { System.out.printf("unexpected char |%s|\n", yytext()); }

0 comments on commit e46e955

Please sign in to comment.