[antlr-interest] Re: Semantic predicates that aren't & hoisting
John D. Mitchell
johnm-antlr at non.net
Sat Mar 12 10:01:26 PST 2005
>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
[...]
Ugh. Well, if you want me to fix your wacky grammar/language, I'll have to
start charging you my consulting rate.
Here's a relatively proper language that deals with statements and
expressions with the nesting that you seem to want.
Here's a sample input: {if (a() < 0) then {b; c(); {;} dd();} e; f();} g();
Note very clearly that the blocks do NOT require a semi-colon where as a
bare expression (when used as a statement) does.
<<<<< ExprList.g <<<<<
/*
* Lex and parse a silly little language showing how to do nesting in a list.
*/
header
{
import java.io.* ;
import antlr.* ;
}
class ExprListParser extends Parser;
options
{
k = 2;
buildAST = false;
}
{
}
program : statementList EOF ;
statementList : ( statement )* ;
statement
: SEMI
| block
| expression SEMI
| "if" LPAREN expression RPAREN "then" statement
( options { warnWhenFollowAmbig = false; } : "else" statement )?
;
block : LCURLY statementList RCURLY ;
expression : equalityExpr ;
equalityExpr : relationalExpr ( ( EQUAL | NOT_EQUAL ) relationalExpr )* ;
relationalExpr : postfixExpr ( ( LT | LTE | GT | GTE ) postfixExpr )* ;
postfixExpr : primaryExpr ( postfixSuffix )? ;
primaryExpr
: ID
| intConst
;
postfixSuffix : ( functionCall )+ ;
functionCall : LPAREN RPAREN ;
protected
intConst : IntLiteral ;
{
import java.io.* ;
import antlr.* ;
}
class ExprListLexer extends Lexer;
options
{
k = 3;
testLiterals = false;
}
{
}
protected
Vocabulary : '\3'..'\377' ;
SEMI : ';' ;
LCURLY : '{' ;
RCURLY : '}' ;
LPAREN : '(' ;
RPAREN : ')' ;
EQUAL : "==" ;
NOT_EQUAL : "!=" ;
LTE : "<=" ;
LT : "<" ;
GTE : ">=" ;
GT : ">" ;
Whitespace
: ( ( '\003'..'\010'
| '\t'
| '\013'
| '\f'
| '\016'..'\037'
| '\177'..'\377'
| ' ' )
| "\r\n" { newline(); }
| ( '\n' | '\r' ) { newline(); }
) { _ttype = Token.SKIP; }
;
protected
Digit : '0'..'9' ;
IntLiteral : ( Digit )+ ;
ID
options { testLiterals = true; }
: ( 'a'..'z' | 'A'..'Z' | '_' )
( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' )*
;
>>>>> >>>>>
<<<<< ExprListTest.java <<<<<
/*
* This example shows how to do a nested list inside an expression.
*/
import java.io.* ;
import antlr.* ;
public class ExprListTest
{
public static void main (String[] args)
{
if (0 >= args.length)
{
spewLine ("Usage: java ExprListTest file+");
}
for (int argNum = 0; argNum < args.length; argNum++)
{
try
{
DataInputStream input = null;
String progName = args[argNum];
if ("--".equals (progName))
{
input = new DataInputStream (System.in);
}
else
{
input = new DataInputStream
( new FileInputStream (progName) );
}
ExprListLexer lexer = new ExprListLexer (input);
ExprListParser parser = new ExprListParser (lexer);
try
{
parser.program();
}
catch (TokenStreamException ex)
{
spewLine ("Unable to read data from '" + progName + "'!",
ex);
}
catch (RecognitionException ex)
{
spewLine ("Unable to parse data from '" + progName + "'!",
ex);
}
}
catch (Exception ex)
{
spewLine ("Unknown exception:", ex);
}
}
}
private static void spewLine (String s, Exception ex)
{
System.err.println (s);
ex.printStackTrace();
System.exit (1);
}
private static void spewLine (String s)
{
System.err.println (s);
System.exit (1);
}
}
>>>>> >>>>>
Take care,
John
More information about the antlr-interest
mailing list