Setrakus
Setrakus

Reputation: 49

Need help compilig an ANTLR grammar file

im new with ANTLR and I don´t know how to compile my grammar. I´m using the v4.4 of ANTLR with a .g4 file that contains:

grammar JayGrammar;
program: KEYWORD_VOI KEYWORD_MAI SEPARATOR_PAB SEPARATOR_PCD (declarations statements);
declarations: (declaration)*;
declaration: (type identifiers);
type: (KEYWORD_INT | KEYWORD_BOO);
identifiers: (IDENTIFIER)*;
statements: (statement)*;
statement: (block | assignment | ifstatement | whilestatementk);
block: SEPARATOR_LAB statements SEPARATOR_LCD;
assignment: (IDENTIFIER OPERATOR_IGU expression);
ifstatement: KEYWORD_IF SEPARATOR_PAB expression SEPARATOR_PCD statement (KEYWORD_ELS statement)?;
whilestatementk: KEYWORD_WHI SEPARATOR_PAB expression SEPARATOR_PCD statement;
expression: conjunction ((OPERATOR_O) conjunction)*;
conjunction: relation ((OPERATOR_Y) relation)*;
relation: addition ((OPERATOR_REL) addition)*;
addition: term ((OPERATOR_SUM|OPERATOR_RES) term)*;
term: negation ((OPERATOR_POR|OPERATOR_DIV) negation)*;
negation:(OPERATOR_NO) factor;
factor: IDENTIFIER|LITERAL|SEPARATOR_PAB expression SEPARATOR_PCD;

    INPUTELEMENT: (WHITESPACE|TOKEN);
    WHITESPACE: (' '|'\t'|'\r'|'\n'|'\f');

    TOKEN: (IDENTIFIER|KEYWORD_BOO|KEYWORD_ELS|KEYWORD_IF|KEYWORD_MAI|KEYWORD_VOI|KEYWORD_WHI|LITERAL
           |SEPARATOR_COM|SEPARATOR_LAB|SEPARATOR_LCD|SEPARATOR_PAB|SEPARATOR_PCD|SEPARATOR_PYC
           |OPERATOR_REL|OPERATOR_DIV|OPERATOR_IGU|OPERATOR_NO|OPERATOR_O|OPERATOR_POR|OPERATOR_RES|OPERATOR_SUM|OPERATOR_Y);

    LITERAL: (BOOLEAN INTEGER);

    KEYWORD_BOO: BOOLEAN;
    KEYWORD_ELS:'else';
    KEYWORD_IF: 'if';
    KEYWORD_INT: 'int';
    KEYWORD_MAI: 'main';
    KEYWORD_VOI: 'void';
    KEYWORD_WHI: 'while';
    BOOLEAN: ('true'|'false');

    INTEGER: (DIGIT+);
    IDENTIFIER: (LETTER (LETTER| DIGIT)*);
    DIGIT: ('0'..'9')+;
    LETTER: ('a'..'z'|'A'..'Z')+;


    SEPARATOR_PAB: '(';
    SEPARATOR_PCD: ')';
    SEPARATOR_LAB: '{';
    SEPARATOR_LCD: '}';
    SEPARATOR_PYC: ';';
    SEPARATOR_COM: ',';


    OPERATOR_IGU: ('=');
    OPERATOR_SUM: ('+');
    OPERATOR_RES: ('-');
    OPERATOR_POR: ('*');
    OPERATOR_DIV: ('/');
    OPERATOR_REL: ('<'|'<='|'>'|'>='|'=='|'!=');
    OPERATOR_Y:   ('&&');
    OPERATOR_O:   ('||');
    OPERATOR_NO:  ('!');

I'll be really glad if someone can tell me how to create the lexer and the parser with the extension ".java" and then compile them to create the classes. I´ve been using NetBeans but i´m really confused, If there is another IDE I can use or anything else, please help me.

Upvotes: 0

Views: 183

Answers (1)

Stephen C
Stephen C

Reputation: 719596

This is explained in the Antlr4 "Getting Started" page.

Doing it by hand is as simple as this:

$ antlr4 YourGrammar.g4
$ javac YourGrammar*.java

... assuming that you have installed and configured a Java JDK and Antlr.


There are a number of ways to use Antrlr with the Netbeans IDE:

  • Just write / generate an Ant build script, and then modify it to include rules for running antlr4 etcetera. With allows you to incorporate Antlr into your builds without any IDE-specific integration.

  • Follow the instructions here on integrating Antlr into Netbeans,

  • Install and use the Antlrworks 2 plugin.

Upvotes: 2

Related Questions