Want more features on Pastebin? Sign Up, it's FREE!
Guest

Flex bison c++

By: a guest on Jun 21st, 2011  |  syntax: C  |  size: 2.39 KB  |  views: 211  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. %{
  2. #include "lista4.tab.hpp"
  3. #include <stdlib.h>
  4. extern int yylex();
  5. %}
  6. %%
  7. "="     {return EQ;}
  8. "!="    {return NE;}
  9. "<"     {return LT;}
  10. ">"     {return GT;}
  11. ":="    {return ASSIGN;}
  12. ";"     {return SEMICOLON;}
  13. "IF"    {return IF;}
  14. "THEN"  {return THEN;}
  15. "ELSE"  {return ELSE;}
  16. "WHILE" {return WHILE;}
  17. "DO"    {return DO;}
  18. "PRINT" {return PRINT;}
  19. "END"   {return END;}
  20. [_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;}
  21. [ \t]+
  22. [0-9]+          {
  23.                 yylval.var = atoi (yytext);
  24.                 return NUMBER;
  25.                 }
  26. [-+/^*'%'()]    {
  27.                 return *yytext;
  28.                 }
  29. \n              return RESULT;
  30. %%
  31.  
  32.  
  33.  
  34. ---------------------------------------------------------
  35.  
  36.  
  37.  
  38.  
  39. %{
  40.   extern "C"
  41.   {
  42.     int yyparse();
  43.     int yylex(void);
  44.     void yyerror(char *s){}
  45.     int yywrap(void){return 1;}
  46.   }
  47.  
  48.   #include <iostream>
  49.   #include <vector>
  50.   #include <string>
  51.   #include <stdlib.h>
  52.   #include <map>
  53.  
  54.   using namespace std;
  55.  
  56.   vector <string> instructions;
  57.  // map <> vars;
  58. %}
  59.  
  60. %union{
  61.   char* text;
  62.   int var;
  63. }
  64.  
  65.  
  66. %token EQ
  67. %token NE
  68. %token LT
  69. %token GT
  70. %token ASSIGN
  71. %token SEMICOLON
  72. %token IF
  73. %token THEN
  74. %token ELSE
  75. %token WHILE
  76. %token DO
  77. %token PRINT
  78. %token END
  79. %token <text> IDENTIFIER
  80. %token <var> NUMBER
  81. %token <var> RESULT
  82. %token READ
  83. %token WRITE
  84.  
  85. %left '+' '-'
  86. %left '*' '/' '%'
  87. %right '^'
  88.  
  89.  
  90. %%
  91. //program: CONST cdeclarations VAR vdeclarations START commands END
  92. //;
  93.  
  94. /*cdeclarations: cdeclarations IDENTIFIER EQ NUMBER
  95. |
  96. ;
  97.  
  98. vdeclarations: vdeclarations IDENTIFIER
  99. |
  100. ;
  101.  
  102. commands: commands command
  103. |
  104. ;*/
  105.  
  106. /*command: IDENTIFIER ASSIGN exp SEMICOLON {}
  107. | IF condition THEN commands ELSE commands END
  108. | WHILE condition DO commands END
  109. | READ IDENTIFIER SEMICOLON
  110. | WRITE IDENTIFIER SEMICOLON
  111. ;*/
  112.  
  113. exp: NUMBER
  114. | IDENTIFIER
  115. | IDENTIFIER "+" IDENTIFIER
  116. | IDENTIFIER "-" IDENTIFIER
  117. | IDENTIFIER "*" IDENTIFIER
  118. | IDENTIFIER "/" IDENTIFIER
  119. | IDENTIFIER "%" IDENTIFIER
  120. ;
  121. /*
  122. condition: IDENTIFIER EQ IDENTIFIER
  123. | IDENTIFIER NE IDENTIFIER
  124. | IDENTIFIER LT IDENTIFIER
  125. | IDENTIFIER GT IDENTIFIER
  126. ;*/
  127.  
  128. %%
  129.  
  130. int main(void){
  131.  // FlexLexer* lexer = new yyFlexLexer;
  132.  
  133.  // while((lexer->yylex)!=0)
  134.  // ;
  135.   yyparse();
  136. }
  137.  
  138.  
  139. ---------------------------------
  140.  
  141.  
  142.  
  143.  
  144. #!/bin/bash
  145. clear
  146. rm launcher lex.yy.cpp *.tab.cpp *.tab.hpp
  147. bison  -d -o lista4.tab.cpp *.y
  148. flex -o lex.yy.cpp *.l
  149. g++ -o launcher *.cpp -lfl
clone this paste RAW Paste Data