Writing a parser for Jzero
The next example is a parser for Jzero, our subset of the Java language. This extends the previous chapter's Jzero example. The big change is the introduction of many context-free grammar rules for more complex syntax constructs than have been seen up to this point. If you wrote a new language not based on an existing one, you would have to come up with the context-free grammar from scratch. For Jzero this is not the case. The grammar we use for Jzero was adapted from a Java dialect named Godiva. To work from a real Java grammar, you can look at https://docs.oracle.com/javase/specs/.
The Jzero lex specification
The Jzero lex
specification is as given in the previous chapter, with a one-line package declaration added to the top. The parser must be generated before the scanner is compiled. This is because yacc
turns j0gram.y
into a parser class whose constant values are referenced from the scanner. Because the static import of yylex()
entails using...