使用ANTLR4进行翻译

时间:2014-10-20 19:32:17

标签: java netbeans antlr antlr4

我使用antlr创建一个解释器,我需要帮助readig文件的每一行(逐个),因为当我使用指令" parser.program()"它只解析文件的一行。  "程序"指令是我的语法文件的第一个解析规则。 我真的需要帮助,我需要创建一个解释器而不是编译器。 我将NetBeans IDE与java一起使用。

`public static void main(String [] args){

    File archivo = new File ("PROYECTO.PSE");

        try{
            FileReader fr = new FileReader (archivo);
            BufferedReader br = new BufferedReader(fr);
            String linea;
            StringTokenizer token;
        while((linea = br.readLine())!=null){    

            token = new StringTokenizer(linea, " ");
            while(token.hasMoreTokens()){
                String palabra = token.nextToken();
                ANTLRInputStream input = new ANTLRInputStream(palabra);
                JayGrammarLexer lexer = new JayGrammarLexer (input);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                JayGrammarParser parser = new JayGrammarParser(tokens);
                ParseTree t = parser.program();
            }  
        }
        }catch(Exception e){
            e.printStackTrace();
        }


}`

语法:

program: KEYWORD_VOI KEYWORD_MAI SEPARATOR_PAB SEPARATOR_PCD SEPARATOR_LAB (declarations           statements) SEPARATOR_LCD;
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 SEPARATOR_LAB statement SEPARATOR_LCD (KEYWORD_ELS SEPARATOR_LAB statement SEPARATOR_LCD)?;
whilestatementk: KEYWORD_WHI SEPARATOR_PAB expression SEPARATOR_PCD SEPARATOR_LAB statement SEPARATOR_LCD;
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|COMMENT|TOKEN);
WHITESPACE: (' '|'\t'|'\r'|'\n'|'\f') -> skip;
COMMENT: ('//'); 
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:  ('!');

0 个答案:

没有答案