Grammatica Reference Manual

Examples

The figure below contains a complete and working grammar file example for a simple arithmetic language.

%header%

GRAMMARTYPE = "LL"

DESCRIPTION = "A grammar for a simple arithmetic language."

AUTHOR      = "Per Cederberg, <per at percederberg dot net>"
VERSION     = "1.0"
DATE        = "10 June 2003"

LICENSE     = "Permission is granted to copy this document verbatim in any
               medium, provided that this copyright notice is left intact."

COPYRIGHT   = "Copyright (c) 2003 Per Cederberg. All rights reserved."


%tokens%

ADD                          = "+"
SUB                          = "-"
MUL                          = "*"
DIV                          = "/"
LEFT_PAREN                   = "("
RIGHT_PAREN                  = ")"
NUMBER                       = <<[0-9]+>>
IDENTIFIER                   = <<[a-z]>>
WHITESPACE                   = <<[ \t\n\r]+>> %ignore%


%productions%

Expression = Term [ExpressionTail] ;

ExpressionTail = "+" Expression
               | "-" Expression ;

Term = Factor [TermTail] ;
     
TermTail = "*" Term
         | "/" Term ;

Factor = Atom
       | "(" Expression ")" ;

Atom = NUMBER
     | IDENTIFIER ;

Figure 1. A grammar for a simple arithmetic language.