解析器和编译器的傻瓜

解析器和编译器的傻瓜

本文介绍了解析器和编译器的傻瓜。从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很好的,但最好的一个在这个领域的一个完整的新手。一个来自较高级别背景的人(VB6,C#,Java,Python) - 不熟悉C或C ++。我在这个阶段对手写解析和Lex / Yacc更感兴趣。

This is a good listing, but what is the best one for a complete newb in this area. One for someone coming from a higher level background (VB6,C#,Java,Python) - not to familiar with C or C++. I'm much more interested in hand-written parsing versus Lex/Yacc at this stage.

如果我只是主修计算机科学而不是心理学,我可能已经采取了类在这在大学。

If I had just majored in Computer Science instead of Psychology I might have taken a class on this in college. Oh well.

推荐答案

请查看:

也很有趣:



  • (确定你已经提到过这个。

  • how to write a programming language
  • parsing where can i learn about it
  • learning resources on parsers interpreters and compilers (ok you already mentioned this one.

可以给出简短的介绍:

第一步是词法分析。字符流被翻译成令牌流,令牌可以是简单的,如==< = + - (etc),它们可以像标识符和数字一样复杂。如果你喜欢,我可以详细说明这一点。

The first step is the lexical analysis. A stream of characters is translated into a stream of tokens. Tokens can be simple like == <= + - (etc) and they can be complex like identifiers and numbers. If you like I can elaborate on this.

下一步是将令牌流翻译成语法树或其他表示。这被称为解析步骤。

The next step is to translate the tokenstream into a syntaxtree or an other representation. This is called the parsing step.

在创建解析器之前,您需要编写语法。例如,我们创建一个表达式解析器:

Before you can create a parser, you need to write the grammar. For example we create an expression parser:

令牌

addOp = '+' | '-';
mulOp = '*' | '/';
parLeft = '(';
parRight = ')';
number = digit, {digit};
digit = '0'..'9';

Each token can have different representations: + and = are both addOp and
23 6643 and 223322 are all numbers.

语言

exp = term | exp, addOp, term;
// an expression is a series of terms separated by addOps.
term = factor | term, mulOp, factor;
// a term is a series of factors separated by mulOps
factor = addOp, factor | parLeft, exp, parRight | number;
// a factor can be an addOp followed by another factor,
// an expression enclosed in parentheses or a number.

词典

我们创建一个状态引擎,遍历char流,创建一个令牌。

We create a state engine that walks through the char stream, creating a token

s00
  '+', '-' -> s01       // if a + or - is found, read it and go to state s01.
  '*', '/' -> s02
  '('      -> s03
  ')'      -> s04
  '0'..'9' -> s05
  whitespace -> ignore and retry  // if a whitespace is found ignore it
  else ERROR      // sorry but we don't recognize this character in this state.
s01
  found TOKEN addOp     // ok we have found an addOp, stop reading and return token
s02
  found TOKEN mulOp
s03
  found TOKEN parLeft
s04
  found TOKEN parRight
s05
  '0'..'9'     -> s05    // as long as we find digits, keep collecting them
  else found number      // last digit read, we have a number

解析器

现在是创建简单解析器/评估器的时候了。这是完整的代码。通常,它们是使用表创建的。但我们保持简单。读取标记并计算结果。

It is now time to create a simple parser/evaluator. This is complete in code. Normally they are created using tables. But we keep it simple. Read the tokens and calculate the result.

ParseExp
  temp = ParseTerm // start by reading a term
  while token = addOp do
    // as long as we read an addop keep reading terms
    if token('+') then temp = temp + ParseTerm  // + so we add the term
    if token('-') then temp = temp - ParseTerm  // - so we subtract the term
  od
  return temp // we are done with the expression

ParseTerm
  temp = ParseFactor
  while token = mulOp do
    if token('*') then temp = temp * ParseFactor
    if token('/') then temp = temp / ParseFactor
  od
  return temp

ParseFactor
  if token = addOp then
    if token('-') then return - ParseFactor  // yes we can have a lot of these
    if token('+') then return ParseFactor
  else if token = parLeft then
    return ParseExpression
    if not token = parRight then ERROR
  else if token = number then
    return EvaluateNumber   // use magic to translate a string into a number

这是一个简单的例子。在实际示例中,您将看到错误处理是解析器的一个重要部分。

This was a simple example. In real examples you will see that error handling is a big part of the parser.

我希望这个澄清一点; - )。

I hope this clarified a bit ;-).

这篇关于解析器和编译器的傻瓜。从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:59