[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Simple syntax parser
Hello all,
I need to implement a simple syntax parser generator. The grammar file
sould have such as this structure:
%header%
HEADER = "some information"
%tokens%
ADD = "+"
SUB = "-"
MUL = "*"
DIV = "/"
LEFT_PAREN = "("
RIGHT_PAREN = ")"
NUMBER = "[0-9]+"
IDENTIFIER = "[a-z]"
WHITESPACE = "[ \t\n\r]+"
%ignore%
WHITESPACE
%productions%
Expression = Term [ExpressionRest] ;
ExpressionRest = "+" Expression
| "-" Expression ;
Term = Factor [TermRest] ;
TermRest = "*" Term
| "/" Term ;
Factor = Atom
| "(" Expression ")" ;
Atom = NUMBER
| IDENTIFIER ;
----- end
I am not sure about the productions and this is my first question. The
assingment is to implement SIMPLE PARSER GENERATOR and I am not sure if
it wont be hard to include features such as repeating (* an +) or
optional elements (such as []). I know it would be much harder to create
a grammar with this syntax but it will be easier to implement it, what
do you think?
Now the main problem. I tried to create a parser in SableCC but with no
luck :( I checked the SableCC's grammar but it is so difficult. Can
anzone send me some easier example of a grammar implemented in SableCC?
Or is anyone able to help me with the productions (header and token
parts are clear to me - the problem is in productions, the strange names
in {} and AST).
Thank you for help
S pozdravem / Best regards
Lukas Zapletal