[antlr-interest] if ( ) {
Denis Washington
dwashington at gmx.net
Mon Jul 31 05:31:39 PDT 2006
Hi,
I have a major problem with the code generation of ANTLR v3. After
generating code for my grammar file (see below), I get the following
error message when compiling:
./SchlichtParser.java:2352: illegal start of expression
if ( ) {
^
I use version 3.0b3.
Here's the grammar file (some bits taken from the Java example grammar):
/*
* schlicht.g
*
* The Schlicht Compiler (Schc)
* Copyright (C) 2006 Denis Washington <dwashington at gmx.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301,
* USA.
*/
grammar SchlichtParser;
options
{
k = 2;
backtrack = true;
memoize = true;
}
///// Compilation Unit
/////////////////////////////////////////////////////////
compilationUnit
: unitBodyDeclaration*
;
unitBodyDeclaration
: programDeclaration
| variableDeclaration
| functionDeclaration
;
///// Program
//////////////////////////////////////////////////////////////////
programDeclaration
: 'program' simpleIdentifier block
;
///// Variables
////////////////////////////////////////////////////////////////
variableDeclaration
: 'var' variableDeclarators ';'
;
variableDeclarators
: variableDeclarator (',' variableDeclarator)*
;
variableDeclarator
: declarator (':=' expression)?
;
declarator
: simpleIdentifier ':' typename
;
///// Functions
////////////////////////////////////////////////////////////////
functionDeclaration
: 'function' simpleIdentifier
'(' functionArguments? ')'
(':' typename)?
block
;
functionArguments
: functionArgument (',' functionArgument)*
;
functionArgument
: declarator
;
///// Type Names
///////////////////////////////////////////////////////////////
typename
: builtInType
;
builtInType
: 'integer'
| 'boolean'
;
///// Statements
///////////////////////////////////////////////////////////////
statement
: block
| declarationStatement
| expressionStatement
;
block
: '{' statement* '}'
;
declarationStatement
: variableDeclaration
;
expressionStatement
: expression ';'
;
///// Expressions
//////////////////////////////////////////////////////////////
expression
: assignmentExpression
;
assignmentExpression
: additiveExpression (':=' assignmentExpression)*
;
additiveExpression
: multiplicativeExpression (('+'|'-') additiveExpression)*
;
multiplicativeExpression
: unaryExpression (('*'|'/') multiplicativeExpression)*
;
unaryExpression
: 'not' unaryExpression
| primaryExpression
;
primaryExpression
: simpleIdentifier
| integerLiteral
| booleanLiteral
;
integerLiteral
: '-'? decimalLiteral
;
booleanLiteral
: 'true'
| 'false'
;
///// Lexical Analysis
/////////////////////////////////////////////////////////
decimalLiteral
: ( '0' | '1'..'9' '0'..'9'* )
;
simpleIdentifier
: (letter|'_'|'$') (letter|digit|'_'|'$')*
;
fragment
letter
: '\u0041'..'\u005a' |
'\u0061'..'\u007a' |
'\u00c0'..'\u00d6' |
'\u00d8'..'\u00f6' |
'\u00f8'..'\u00ff' |
'\u0100'..'\u1fff' |
'\u3040'..'\u318f' |
'\u3300'..'\u337f' |
'\u3400'..'\u3d2d' |
'\u4e00'..'\u9fff' |
'\uf900'..'\ufaff'
;
fragment
digit
: '\u0030'..'\u0039' |
'\u0660'..'\u0669' |
'\u06f0'..'\u06f9' |
'\u0966'..'\u096f' |
'\u09e6'..'\u09ef' |
'\u0a66'..'\u0a6f' |
'\u0ae6'..'\u0aef' |
'\u0b66'..'\u0b6f' |
'\u0be7'..'\u0bef' |
'\u0c66'..'\u0c6f' |
'\u0ce6'..'\u0cef' |
'\u0d66'..'\u0d6f' |
'\u0e50'..'\u0e59' |
'\u0ed0'..'\u0ed9' |
'\u1040'..'\u1049'
;
whitespace
: (' '|'\r'|'\t'|'\u000C'|'\n') { channel = 99; }
;
comment
: '/*' ( options { greedy = false; }: . )* '*/' { channel = 99; }
;
lineComment
: '//' ~('\n'|'\r')* '\r'? '\n' { channel = 99; }
;
More information about the antlr-interest
mailing list