How to write a simple Lexer/Parser with antlr 2.7?
- by Burkhard
Hello,
I have a complex grammar (in antlr 2.7) which I need to extend. Having never used antlr before, I wanted to write a very simple Lexer and Parser first.
I found a very good explanation for antlr3 and tried to adapt it:
header{
#include <iostream>
using namespace std;
}
options {
language="Cpp";
}
class P2 extends Parser;
/* This will be the entry point of our parser. */
eval
: additionExp
;
/* Addition and subtraction have the lowest precedence. */
additionExp
: multiplyExp
( "+" multiplyExp
| "-" multiplyExp
)*
;
/* Multiplication and addition have a higher precedence. */
multiplyExp
: atomExp
( "*" atomExp
| "/" atomExp
)*
;
/* An expression atom is the smallest part of an expression: a number. Or
when we encounter parenthesis, we're making a recursive call back to the
rule 'additionExp'. As you can see, an 'atomExp' has the highest precedence. */
atomExp
: Number
| "(" additionExp ")"
;
/* A number: can be an integer value, or a decimal value */
number
: ("0".."9")+ ("." ("0".."9")+)?
;
/* We're going to ignore all white space characters */
protected
ws
: (" " | "\t" | "\r" | "\n") { newline(); }
;
It does generate four files without errors: P2.cpp, P2.hpp, P2TokenTypes.hpp and P2TokenTypes.txt. But now what? How do I create a working programm with that? I tried to add these files to a VS2005-WinConsole-Project but it does not compile:
p2.cpp(277) : fatal error C1010:
unexpected end of file while looking
for precompiled header. Did you forget
to add '#include "stdafx.h"' to your
source?