Summary CS 101 Description of the JavaCC Grammar File
4 views 0 purchase
Course
CS 101 Description of the JavaCC Grammar File
Institution
CS 101 Description Of The JavaCC Grammar File
CS 101 Description of the JavaCC Grammar File
This web page contains the complete syntax of Java Compiler Compiler grammar files with detailed explanations of each construct.
Tokens in the grammar files follow the same conventions as for Java. Hence identifiers, strings, characters, etc. use...
cs 101 description of the javacc grammar file this web page contains the complete syntax of java compiler compiler grammar files with detailed explanations of each construct tokens in the grammar
Written for
CS 101 Description of the JavaCC Grammar File
All documents for this subject (11)
Seller
Follow
magdamwikash23
Reviews received
Content preview
CS 101 Description of the JavaCC
Grammar File
This web page contains the complete syntax of Java Compiler Compiler grammar files with
detailed explanations of each construct.
Tokens in the grammar files follow the same conventions as for Java. Hence identifiers,
strings, characters, etc. used in the grammars are the same as Java identifiers, Java strings,
Java characters, etc.
White space in the grammar files also follows the same conventions as for Java. This includes
the syntax for comments. Most comments present in the grammar files are generated into the
generated parser/lexical analyzer.
Grammar files are preprocessed for Unicode escapes just as Java files are (i.e., occurrences of
strings such as \uxxxx - where xxxx is a hex value - are converted the the corresponding
Unicode character before lexical analysis).
Exceptions to the above rules: The Java operators "<<", ">>", ">>>", "<<=", ">>=", and
">>>=" are left out of Java Compiler Compiler's input token list in order to allow convenient
nested use of token specifications. Finally, the following are the additional reserved words in
the Java Compiler Compiler grammar files.
EOF IGNORE_CASE JAVACODE LOOKAHEAD
MORE options PARSER_BEGIN PARSER_END
SKIP SPECIAL_TOKEN TOKEN TOKEN_MGR_DECLS
Any Java entities used in the grammar rules that follow appear italicized with the prefix java_
(e.g., java_compilation_unit).
The grammar file starts with a list of options (which is optional). This is then followed by a
Java compilation unit enclosed between "PARSER_BEGIN(name)" and "PARSER_END(name)".
After this is a list of grammar productions. Options and productions are described later.
The name that follows "PARSER_BEGIN" and "PARSER_END" must be the same and this
identifies the name of the generated parser. For example, if name is "MyParser", then the
following files are generated:
MyParser.java: The generate parser.
MyParserTokenManager.java: The generated token manager (or scanner/lexical analyzer).
MyParserConstants.java: A bunch of useful constants.
, Other files such as "Token.java", "ParseError.java", etc. are also generated. However, these
files contain boilerplate code and are the same for any grammar and may be reused across
grammars.
Between the PARSER_BEGIN and PARSER_END constructs is a regular Java compilation unit (a
compilation unit in Java lingo is the entire contents of a Java file). This may be any arbitrary
Java compilation unit so long as it contains a class declaration whose name is the same as the
name of the generated parser ("MyParser" in the above example). Hence, in general, this part
of the grammar file looks like:
JavaCC does not perform detailed checks on the compilation unit, so it is possible for a
grammar file to pass through JavaCC and generate Java files that produce errors when they
are compiled.
If the compilation unit includes a package declaration, this is included in all the generated
files. If the compilation unit includes imports declarations, this is included in the generated
parser and token manager files.
The generated parser file contains everything in the compilation unit and in addition contains
the generated parser code that is included at the end of the parser class. For the above
example, the generated parser will look like:
The generated parser includes a public method declaration corresponding to each non-terminal
(see javacode_production and bnf_production) in the grammar file. Parsing with respect to a
non-terminal is achieved by calling the method corresponding to that non-terminal. Unlike
yacc, there is no single start symbol in JavaCC - one can parse with respect to any non-
terminal in the grammar.
The generated token manager provides one public method:
Token getNextToken() throws ParseError;
For more details on how this method may be used, please read the description of the Java
Compiler Compiler API.
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller magdamwikash23. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $13.26. You're not tied to anything after your purchase.