How to write a simple Lexer / Parser with antlr 2.7?
I have a complex grammar (in antlr 2.7) that I need to expand on. I have never used antlr before, I wanted to write a very simple Lexer and Parser.
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 generates four files without errors: P2.cpp, P2.hpp, P2TokenTypes.hpp and P2TokenTypes.txt. But now what? How do I create a work program? I tried adding these files to VS2005-WinConsole-Project but it won't compile:
p2.cpp (277): fatal error C1010: Unexpected end of file while searching for precompiled header. Did you forget to add '#include' stdafx.h "'to your source?
a source to share
check out these examples, there are some C examples that should help you along the way.
By the way, the error message comes from the fact that you are compiling with pre-compiled headers, so it wants stdafx.h to be included at the beginning of your .cpp file, you can add this to the grammar in the header section {}.
a source to share