简体   繁体   English

词汇分析

[英]Lexical Analysis

My target is to make a very simple and basic C syntax checker. 我的目标是制作一个非常简单和基本的C语法检查器。 (Not a full Compiler but just a basic Program which take a source code as Input and would print out the code back showing the errors). (不是一个完整的编译器,而只是一个基本程序,它将源代码作为输入,并打印出显示错误的代码)。 I want to use C++ as the language for this. 我想用C ++作为语言。

Can anyone guide me to write regular expression in 'c++' language for the following: 任何人都可以指导我用'c ++'语言编写正则表达式,如下所示:

  1. Assignment 分配
  2. For 对于
  3. switch 开关

Any details/suggestions/guidance further would be much appreciated. 任何细节/建议/指导将进一步非常感谢。

I don't think you can parse C with regular expressions alone. 我不认为你可以单独用正则表达式解析C You'll need to start looking into lexers, parsers, grammars, etc... 你需要开始研究词法分析器,解析器,语法等...

A good starting point could be this: Quick Starter on Parser Grammars - No Past Experience Required . 一个很好的起点可能是这样: Parser Grammars的快速入门 - 无需过往经验

You can find C grammars online for yacc / lex . 你可以找到C语法在线yacc / lex

Checking the syntax of C code requires a lot more than regular expressions. 检查C代码的语法比正则表达式需要更多。 You'll need a tool that supports parsing algebraic gammars instead, I'd suggest looking at http://www.gnu.org/software/bison/ which will generate the "skeleton" of a parser in C++ so you can edit it and add your syntax-checking code. 你需要一个支持解析代数伽玛的工具,我建议你查看http://www.gnu.org/software/bison/ ,它将在C ++中生成解析器的“骨架”,这样你就可以编辑它了。并添加语法检查代码。

Use the clang libraries. 使用clang库。 A tutorial can be found on GitHub . 可以在GitHub上找到一个教程。

尝试提升精神

To recognize C statements, lexical analysis is not sufficient, because lexers work at the character level. 要识别C语句,词法分析是不够的,因为词法分析器在角色级别工作。 What you need, in addition to a C lexer, is a C parser which will do syntax analysis of the source code. 除了C词法分析器之外,你需要的是一个C语法分析器,它将对源代码进行语法分析。 The LRSTAR Parser Generator supplies a C project which will get you up and running. LRSTAR Parser Generator提供了一个C项目,可以帮助您启动和运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM