Professional Documents
Culture Documents
2x Courseware | edX
import lib6005.parser;
The second step is to de ne an Enum type that contains all the terminals and non-
terminals used by your grammar. This will tell the compiler which de nitions to expect
in the grammar and will allow it to check for any missing ones.
Note that ParserLib itself is case insensitive, but by convention, the names of enum
values are all upper case.
From within your code, you can create a parser by calling the compile static method in
GrammarCompiler.
...
Parser<IntegerGrammar> parser =
GrammarCompiler.compile(new File("IntegerExpression.g"),
IntegerGrammar.ROOT);
https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/04-Parser-Generators/?child=first 1/2
5/10/2020 Generating the parser | Reading 4: Parser Generators | 6.005.2x Courseware | edX
Assuming you don’t have any syntax errors in your grammar le, the result will be a
Parser object that can be used to parse text in either a string or a le. Notice that the
Parser is a generic type that is parameterized by the enum you de ned earlier.
PS1 Problem 2
GrammarCompiler.compile doesn't parse in Expression.g ,
https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/04-Parser-Generators/?child=first 2/2