[antlr-interest] Plz help newbie with simple issue porting from JavaCC

tdjastrzebski tdjastrzebski at yahoo.com
Sat Jun 28 05:24:03 PDT 2003


Hi everybody, since I enjoy Antlr more and more I decided to port 
some simple parser written originally in JavaCC to Antlr.
Her is an issue which has immediately shown up.

expression :
        subExpression ( binaryOperator subExpression )*
	;
subExpression :
	( LPAREN expression RPAREN )
	| function
	;
function : //or constant
	Identifier (LPAREN (expression)? RPAREN)?
	;

Above grammar does not parse expressions containing single identifier 
without parenthesis: e.g. "PI" and I do not understand why.
Even if I parse such a string using just "function" rule "unexpected 
token" error occurs.

I would appreciate any pointers, TIA
Tom Jastrzebski

-----------------------------------------------
grammar file:

options {
	language = "Java";
}

// PARSER 
**********************************************************************

class TestParser extends Parser;

options {
	buildAST = true;
	k = 3;
}

expression
	: subExpression ( binaryOperator subExpression )*
	;

subExpression
	:
	( LPAREN expression RPAREN )
	| function
	;

function
	:
	Identifier (LPAREN (expression)? RPAREN)?
	;

binaryOperator
	: PLUS | MINUS | STAR | DIV
	;

// LEXER 
**********************************************************************
*

class TestLexer extends Lexer;

options {
	testLiterals = false;
    k = 3;
    caseSensitive = false;
    caseSensitiveLiterals = false;
}

LPAREN: '(' ;
RPAREN: ')' ;
PLUS: '+' ;
MINUS: '-' ;
STAR: '*' ;
DIV: '/' ;

Whitespace
	: (' ' | '\t' | '\n' | '\r')
	{ _ttype = Token.SKIP; }
	;

Identifier
	: ('a'..'z')+
	;

--------------------------------------------------
Main.java:

import java.io.*;
import antlr.*;

class Main
{
	public static void main(String[] args)
	{
		LexerSharedInputState input = null;
        FileInputStream inputStream = null;
        InputStreamReader reader = null;
		String fileName;

		if (args.length > 0) {
			fileName = args[0];

            try {
                inputStream = new FileInputStream(fileName);
            } catch (FileNotFoundException e) {
                System.out.println("File " + fileName + " not 
found.");
                System.exit(1);
            }
            try {
                reader = new InputStreamReader(inputStream, "SJIS");
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Invalid encoding");
                System.exit(1);
            }
            input = new antlr.LexerSharedInputState(new CharBuffer
(reader));

		} else {
			fileName = "<stdin>";
			input = new antlr.LexerSharedInputState(new 
DataInputStream(System.in));
		}

		try {
			TestLexer lexer = new TestLexer(input);
			lexer.setFilename(fileName);

			TestParser parser = new TestParser(lexer);
			parser.setFilename(fileName);

			// Parse the input expression
            //parser.expression();

            parser.function();

			CommonAST t = (CommonAST)parser.getAST();
			// Print the resulting tree out in LISP 
notation
			if (t != null) {
				System.out.println(t.toStringTree());
			}

		} catch(TokenStreamException e) {
			System.err.println("exception: " + e);
		} catch(RecognitionException e) {
			System.err.println("exception: " + e);
		}

		System.out.println("press any key ...");
		try {
			System.in.read();
            System.exit(0);
		} catch (java.io.IOException e) {}
	}
}


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list