项目作者: alsaghir

项目描述 :
Recursive descent parser using c++
高级语言: C++
项目地址: git://github.com/alsaghir/cppParser.git
创建时间: 2017-05-06T17:11:14Z
项目社区:https://github.com/alsaghir/cppParser

开源协议:

下载


c++ Parser

Recursive descent parser using c++

The language context free rules are

  1. Program --> Statement_list
  2. Statement_list --> Statement_list Statement | Statement
  3. Statement --> Var_statement | If_statement
  4. Var_statement --> (int|void)ID | (int|void)ID[num];
  5. If_statement --> if (Expression) Statement_list | if (Expression) Statement_list else Statement_list
  6. Expression --> Var=Expression
  7. Var --> ID|ID[num]
  8. Expression --> add-expression Relop add-expression | add-expression
  9. Relop --> >=,<=,<>,>,<,==
  10. add-expression --> add-expression addop term | term
  11. addop --> +|-
  12. term --> term mulop factor | factor
  13. mulop --> *|/
  14. factor --> num

ID, num are terminal classes. The rest (int, void, else, <=, =..etc) of terminals are lexemes.

To be able to work using recursive descent, modified rules presented

  1. Program --> Statement_list
  2. Statement_list --> Statement Statement_list_dash
  3. Statement_list_dash --> Statement Statement_list_dash | ε
  4. Statement --> Var_statement | If_statement
  5. Var_statement --> ( IV ) Var
  6. IV --> int | void
  7. Var --> id | id[num];
  8. If_statement --> if (Expression) Statement_list else Statement_list | if (Expression) Statement_list
  9. Expression --> Var=Expression | add-expression Relop add-expression | add-expression
  10. Relop --> >= | <= | <> | > | < | ==
  11. add-expression --> term add-expression_dash
  12. add-expression_dash --> addop term add-expression_dash | ε
  13. addop --> +|-
  14. term --> factor term_dash
  15. term_dash --> mulop factor term_dash | ε
  16. mulop --> *|/
  17. factor --> num

Input are tokens in specific format

Take a look to Sample.hamada for example and here are some examples. These examples should be valid according to the language and produce success on parsing.

Example 1:

  1. <kw, if>
  2. <oc, (>
  3. <num, 4>
  4. <eq, ==>
  5. <num, 3>
  6. <cc, )>
  7. <oc, (>
  8. <kw, int>
  9. <cc, )>
  10. <id, aa>
  11. <os, [>
  12. <num, 66>
  13. <cs, ]>
  14. <kw, else>
  15. <oc, (>
  16. <kw, void>
  17. <cc, )>
  18. <id, bb>

Example 2:

  1. <oc, (>
  2. <kw, int>
  3. <cc, )>
  4. <id, bb>
  5. <os, [>
  6. <num, 66>
  7. <cs, ]>
  8. <semi, ;>

More output

light-output.cpp & extensiveOutput.cpp are completely commented but saved for sake of testing ONLY. They have to much outputs on each function calling and more.