views:

153

answers:

1

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?

A: 

look at these examples, there are some C examples which should help you on the way.

BTW the error message comes from that you are compiling with precompiled headers so it wants the stdafx.h include at the start of your .cpp file, you can add this in the grammar in the header{ } section.

Anders K.
Am I mistaken or is it for antlr 3?
Burkhard
@Burkhard, yes, all are v3 grammars.
Bart Kiers
Then it does not help me a lot.
Burkhard
Sorry mate, maybe you anyway can get some inspiration from there (to rewrite for v3 :-)
Anders K.
I probably won't get a better answer... :(
Burkhard