我有一个复杂的语法(在 antlr 2.7 中)需要扩展。以前从未使用过 antlr,我想先写一个非常简单的 Lexer 和 Parser。
我找到了一个很好的antlr3解释并尝试改编它:
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(); }
;
它确实生成了四个没有错误的文件:P2.cpp、P2.hpp、P2TokenTypes.hpp 和 P2TokenTypes.txt。但现在呢?我如何用它创建一个工作程序?我试图将这些文件添加到 VS2005-WinConsole-Project 但它没有编译:
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?
最佳答案
看these示例,这里有一些 C 示例应该对您有所帮助。
顺便说一句,错误消息来自于您正在使用预编译的 header 进行编译,因此它希望将 stdafx.h 包含在您的 .cpp 文件的开头,您可以将其添加到 header {}部分的语法中。
关于c++ - 如何使用 antlr 2.7 编写简单的 Lexer/Parser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2688642/