Advertisement
MMRivers

p.y

Oct 25th, 2019
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %{
  2. #include <stdio.h> /* printf() */
  3. #include "common.h" /* MAX_STR_LEN */
  4. int yylex(void);
  5. void yyerror(const char *txt);
  6. %}
  7.  
  8.  
  9. /* Declaration of terminal symbols */
  10. %token KW_PROGRAM KW_BEGIN KW_END KW_USES KW_VAR KW_CONST KW_IF KW_THEN KW_ELSE
  11. %token KW_CHAR KW_INTEGER KW_REAL KW_FOR KW_TO KW_DO KW_FUNCTION
  12. %token KW_PROCEDURE KW_DOWNTO KW_ARRAY KW_RECORD KW_OF
  13. %token ASSIGN LE RANGE
  14. %token IDENT STRING_CONST FLOAT_CONST INTEGER_CONST CHARACTER_CONST
  15.  
  16. %union
  17. { /* Declaration of token types */
  18.     char s[ MAX_STR_LEN + 1 ]; /* text fields for names etc. */
  19.     int i; /* interger field */
  20.     double d; /* floating point field */
  21. }
  22.  
  23. %%
  24.  
  25. Grammar: /* empty */
  26.     | TOKENS
  27. ;
  28.    
  29. TOKENS: TOKEN
  30.     | TOKENS TOKEN
  31. ;
  32.  
  33. TOKEN: KEYWORD | OPERATOR | IDENT | CONSTS | CHARS | error
  34. ;
  35.  
  36. KEYWORD: KW_PROGRAM | KW_BEGIN | KW_END | KW_USES | KW_VAR | KW_CONST | KW_IF
  37.     | KW_THEN | KW_ELSE | KW_CHAR | KW_INTEGER | KW_REAL | KW_FOR | KW_TO
  38.         | KW_DO | KW_FUNCTION | KW_PROCEDURE | KW_DOWNTO | KW_ARRAY | KW_RECORD | KW_OF
  39. ;
  40.  
  41. OPERATOR: ASSIGN | LE | RANGE
  42. ;
  43.  
  44. CONSTS: STRING_CONST | FLOAT_CONST | INTEGER_CONST | CHARACTER_CONST
  45. ;
  46.  
  47. CHARS:  '+' | '-' | '*' | '/' | ';' | ',' | '=' | ':' | '.'
  48.     | '(' | ')' | '{' | '}' | '[' | ']'
  49. ;  
  50.  
  51. %%
  52.  
  53.  
  54. int main( void )
  55. {
  56.     int ret;
  57.     printf( "Author: Krzysztof Święcicki\n" );
  58.     printf( "yytext              Token type      Token value as string\n\n" );
  59.     /* invocation of the parser */
  60.     ret = yyparse();
  61.     return ret;
  62. }
  63.  
  64. void yyerror( const char *txt )
  65. {
  66.     printf( "Syntax error %s", txt );
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement