c++ - 如何使用 antlr 2.7 编写简单的 Lexer/Parser?

标签 c++ visual-studio-2005 antlr

我有一个复杂的语法(在 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/

相关文章:

c++ - 需要帮助使用 libpng 读取图像

c++ - 我如何与父 namespace 中的模板类成为 friend ?

c++ -/MT 和/MD 构建崩溃,但仅当未附加调试器时才会崩溃 : how to debug?

c# - Visual Studio 中的 ANTLR 语法高亮 DSL

python - 如何检查语句中操作的类型?

c++ - 使用 FFmpeg 将原始 RGB32 文件转换为 JPEG 或 PNG

c++ - 我想我可能想出了一个数组类型右值的例子

c++ - 为什么只有C++会出现数据类型转换错误,而C不会出现数据类型转换错误?

c++ - Visual Studio 跳过构建

java - ANTLR 生成空条件