Java implemention with bnf does not generate valid Java
See original GitHub issueTrying out the new Java support. My uses will want to use yacc/bison flavor of grammar. Given:
./bin/syntax -m LALR1 --grammar ../syntax_test/src/com/syntax/calc.bnf -o ../syntax_test/src/com/syntax/Parser.java -w
Here is the .bnf:
%left '+'
%left '*'
%%
exp
: exp '+' exp
/* Explicitly calculate location */
{ $$ = new BinaryExpression("+", $1, $3); }
| exp '*' exp
/* Use default result location: @$ */
{ $$ = new BinaryExpression("*", $1, $3) }
| '(' exp ')'
{ $$ = $2 }
| number
/* Named args and position */
{ $$ = new NumericLiteral(Integer.parseInt((String) $number)) }
;
literal
: number
;
number
: number digit
{ $$ = (String) $number + (String) $digit; }
| digit
;
digit : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
Generating this will show two immediate issues:
_lexRule0 has no lhs for semanticValue = null;
String _lexRule0() {
.semanticValue = null;
}
The classes generated need to be inner classes to be valid. Which led me to add the following to the top of the file:
%{
class Parser {
%}
Unfortunately, I could not figure out how to add the closing ‘}’. In yacc I would solve this with adding:
%%
}
underneath the grammar section but this grammar does not seem to like that.
If I manually add that final ‘}’ then it exposes a second tier issue that there are static method declarations in inner classes which are not marked static.
Not sure how much I should have broken this up for the report. I figure I would dump all I noticed here. Let me know if I can report stuff differently. I am sure once the basics are working I will be able to make more granular reports.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Wacky. I don’t get it … it now works. All the inner class stuff does not seem to be issue. I am fairly confused now. My only thought was I used npm build from master vs actual release. I guess though I have no current issues after 89996de.
@DmitrySoshnikov as to your other suggestion I will try changing the source so I can plug in a lexer. It should be good enough for a test.
I might suggest that a character-based parser in Java should maybe work with a Reader instead of a String, but that may be a future enhancement issue I can open (unfortunately Ruby supports encodings that Java charsets do not support so we do everything with raw bytes (or InputStream)). Decoupling lexer as interface would definitely allow switching between different input types…but again a feature request.
Thanks for your work!
@DmitrySoshnikov I will follow those step by step and let you know but I gave instructions and file with what I used above and they are not much different. I do not see a sizable difference between the two unless the -w did something odd? I will play a little bit…