[antlr-interest] White spaces not allowed
Dominic Tardif
Dominic.Tardif at USherbrooke.ca
Mon Jan 12 11:53:58 PST 2009
Hello everyone! I've been working on this grammar for quite some time now, and
it works quite well except for one little detail: white spaces are not allowed.
Since the input is user-created, it's supposed to be "user-friendly", thus
more readable with spaces between each elements. I really don't understand this
because I've declared the white spaces to be skipped. Any help on this issue
would be greatly appreciated! Here is the input followed by the grammar:
------------
input
------------
#LIB::Logic
sample(a,b,c;g,h;k){
g=a*b;
h=c'+g*k;
k=!g+c;
}
#ENDLIB
------------
grammar
------------
grammar Logic;
options {
output = AST;
ASTLabelType = CommonTree;
}
tokens {
INV = '\'';
STMT_END = ';';
}
@header {
import java.util.HashMap;
}
@members {
HashMap<String,Integer> memory = new HashMap<String,Integer>();
}
circuit: (library {System.out.println($library.tree.toStringTree());})+
;
library: lib='#LIB::' ID (NEWLINE)? function+ '#ENDLIB' -> ^(LIBRARY ID
^(FLIST[$lib, "FLIST "] function+))
;
function: user_def bloc (NEWLINE)? -> ^(FCT function_id bloc)
;
bloc: lc='{' stmt+ '}' -> ^(SLIST[$lc, "SLIST"] stmt+)
;
function_id: ID '(' inparam? ';' outparam ';' ioparam? ')' -> ^(ID ^(INPARAM
inparam)? ^(OUTPARAM outparam) ^(IOPARAM ioparam)?)
;
inparam: (ids+=ID) (coma=',' ids+=ID)* -> ID+
;
outparam: (ids+=ID) (coma=',' ids+=ID)* -> ID+
;
ioparam: (ids+=ID) (coma=',' ids+=ID)* -> ID+
;
stmt: ID ' ' function_id STMT_END -> ^(STMT ID function_id)
| ID '=' expr STMT_END -> ^('=' ID expr)
| NEWLINE ->
;
expr: multExpr ('+'^ multExpr)*
;
multExpr: phase (('*'^|' '^) phase)*
;
phase: atom (INV)? -> ^(PHASE INV? atom)
| '!' atom -> ^(PHASE '\'' atom)
;
atom: ID
| '(' expr ')'
;
ID: ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|'0'..'9')* | INT;
fragment
INT: '0'|'1';
NEWLINE: ('\r'? '\n')+;
WS: (' '|'\t'|'\r'|'\n')+ {skip();};
COMMENT : '//' ( options{greedy=false;}: . )* '\n' {skip();}
| '/*' ( options{greedy=false;}: . )* '*/' {skip();};
More information about the antlr-interest
mailing list