You are on page 1of 2

5/10/2020 Generating the parser | Reading 4: Parser Generators | 6.005.

2x Courseware | edX

Course  Readings  Readin…  Genera…

Generating the parser


Generating the parser
The rest of this reading will use as a running example the IntegerExpression grammar
de ned earlier, which we’ll store in a le called IntegerExpression.g.

The ParserLib parser generator tool converts a grammar source le like


IntegerExpression.g into a parser. In order to do this, you need to follow three steps.
First, you need to import the ParserLib library, which resides in a package
lib6005.parser:

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.

enum IntegerGrammar {ROOT, SUM, PRIMITIVE, NUMBER, WHITESPACE};

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

This code opens the le IntegerExpression.g and compiles it using the


GrammarCompiler into a Parser object. The compile method takes as a second
argument the name of the nonterminal to use as the entry point of the grammar; root
in the case of this example.

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.

Discussion Hide Discussion


Topic: Reading 4 / Generating the parser

Show all posts by recent activity

 IOException when parsing example le 4


Any idea why this isn't opening?: java.io.IOException: Cannot read from le C:\ps1-expressivo\src…

 The grammar of a GrammarCompiler grammer 1


At the beginning of the [GrammarCompiler class API documentation][1], there is a section that o…

 Could someone post how this should look in a le to get it to run?


11
I haven't been able to get the compiler to work. I downloaded PS1, and made a java class to run t…

 Missing Information: Where to start on Reading 4 4


I take it we're supposed to follow along, building a .java le and a .g le for this reading. Where to…

 Where is the enum ideally supposed to be declared? 8


I expected to nd a stub.

 GrammarCompiler.compile() not allowing the creation of a new File(....)??? 8

 Unable to import ParserLib library 5


Hi, Following the instructions to import lib6005.parser give an error in Eclipse: The import lib600…

 PS1 Problem 2
GrammarCompiler.compile doesn't parse in Expression.g ,

© All Rights Reserved

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/04-Parser-Generators/?child=first 2/2

You might also like