example1.y

来自「学习lemon语法分析的windows程序」· Y 代码 · 共 69 行

Y
69
字号
/* Copyright (GPL) 2004 mchirico@users.sourceforge.net or mchirico@comcast.net  Simple lemon parser  example.      $ ./lemon example1.y                            The above statement will create example1.c.  The command below  adds  main and the  necessary "Parse" calls to the  end of this example1.c.    $ cat <<EOF >>example1.c                          int main()                                        {                                                   void* pParser = ParseAlloc (malloc);              Parse (pParser, INTEGER, 1);                      Parse (pParser, PLUS, 0);                         Parse (pParser, INTEGER, 2);                      Parse (pParser, 0, 0);                            ParseFree(pParser, free );                       }                                                EOF                                                            $ g++ -o ex1 example1.c                                           $ ./ex1  See the Makefile, as most all of this is  done automatically.    Downloads:  http://prdownloads.sourceforge.net/souptonuts/lemon_examples.tar.gz?download*/%token_type {int}     %left PLUS MINUS.   %left DIVIDE TIMES.     %include {   #include <iostream>  #include "example1.h"}     %syntax_error {    std::cout << "Syntax error!" << std::endl;  }      program ::= expr(A).   { std::cout << "Result=" << A << std::endl; }     expr(A) ::= expr(B) MINUS  expr(C).   { A = B - C; }  expr(A) ::= expr(B) PLUS  expr(C).   { A = B + C; }  expr(A) ::= expr(B) TIMES  expr(C).   { A = B * C; }  expr(A) ::= expr(B) DIVIDE expr(C).  {          if(C != 0){           A = B / C;          }else{           std::cout << "divide by zero" << std::endl;           }}  /* end of DIVIDE */expr(A) ::= INTEGER(B). { A = B; } 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?