JavaCC Grammar Definition and Compilation | Generated by AI

Home PDF

I’ll create a comprehensive artifact that demonstrates JavaCC (Java Compiler Compiler) with examples of grammar definition, variable declarations, and compilation.

/* Basic JavaCC Grammar Example */

// Options and parser class declaration
options {
    STATIC = false;  // Allow multiple parser instances
    LOOKAHEAD = 2;   // Set lookahead to 2 tokens
}

PARSER_BEGIN(SimpleParser)
public class SimpleParser {
    public static void main(String[] args) throws ParseException {
        SimpleParser parser = new SimpleParser(System.in);
        parser.Start();
    }
}
PARSER_END(SimpleParser)

// Lexical Specifications (Tokens)
TOKEN : {
    // Keywords
    < VAR: "var" >
    < INT: "int" >
    < BOOLEAN: "boolean" >
    < STRING: "string" >

    // Literals and Identifiers
    < NUMBER: (["0"-"9"])+ >
    < IDENTIFIER: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","0"-"9","_"])* >

    // Operators and Punctuation
    < ASSIGN: "=" >
    < SEMICOLON: ";" >
    < PLUS: "+" >
    < MINUS: "-" >
    < MULTIPLY: "*" >
    < DIVIDE: "/" >
    < LPAREN: "(" >
    < RPAREN: ")" >
}

// Skip whitespace and comments
SKIP : {
    " " | "\t" | "\n" | "\r"
    | <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
    | <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/">
}

// Grammar Rules
void Start() : {} {
    (Statement())* <EOF>
}

void Statement() : {} {
    VariableDeclaration()
    | Expression()
    | AssignmentStatement()
}

void VariableDeclaration() : {} {
    // Syntax: var [type] [identifier] (= [expression])?;
    <VAR> Type() <IDENTIFIER> 
    ( <ASSIGN> Expression() )? 
    <SEMICOLON>
}

void Type() : {} {
    <INT> | <BOOLEAN> | <STRING>
}

void Expression() : {} {
    AdditiveExpression()
}

void AdditiveExpression() : {} {
    MultiplicativeExpression() 
    (
        <PLUS> MultiplicativeExpression()
        | <MINUS> MultiplicativeExpression()
    )*
}

void MultiplicativeExpression() : {} {
    PrimaryExpression() 
    (
        <MULTIPLY> PrimaryExpression()
        | <DIVIDE> PrimaryExpression()
    )*
}

void PrimaryExpression() : {} {
    <NUMBER>
    | <IDENTIFIER>
    | <LPAREN> Expression() <RPAREN>
}

void AssignmentStatement() : {} {
    <IDENTIFIER> <ASSIGN> Expression() <SEMICOLON>
}

Now, I’ll provide a comprehensive explanation of JavaCC and how to work with this grammar:

JavaCC Overview

JavaCC (Java Compiler Compiler) is a parser generator that helps you create parsers and tokenizers for custom languages or domain-specific languages.

Key Components in the Example:

  1. Options Block
    • STATIC = false: Allows creating multiple parser instances
    • LOOKAHEAD = 2: Sets parser to look ahead 2 tokens for disambiguation
  2. Token Definitions
    • Defines tokens like keywords, identifiers, numbers, and operators
    • Uses regular expressions to match token patterns
    • Example tokens:
      • < NUMBER: (["0"-"9"])+ > matches one or more digits
      • < IDENTIFIER: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","0"-"9","_"])* > matches variable names
  3. Skip Specifications
    • Defines what to ignore during parsing (whitespace, comments)
    • Includes single-line and multi-line comment handling
  4. Grammar Rules
    • Define the syntax and structure of the language
    • Use recursive descent parsing
    • Rules like VariableDeclaration(), Expression() define language constructs

Compilation and Usage Steps

  1. Generate Parser
    javacc SimpleParser.jj
    

    This command generates:

    • SimpleParserConstants.java
    • SimpleParserTokenManager.java
    • ParseException.java
    • SimpleParser.java
  2. Compile Generated Files
    javac *.java
    
  3. Run the Parser
    java SimpleParser
    

Example Input

var int x = 10;
var int y = 20;
x = x + y;

Features Demonstrated

Limitations of This Example

Advanced Usage Tips

Would you like me to elaborate on any specific aspect of JavaCC or this grammar example?


Back 2025.03.25 Donate