Advertisement
Mikescher

oberon.pars [1]

Nov 26th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. /* Project:  COCKTAIL training
  2.  * Descr:    A simple pocket computer (scanner, parser, evaluator)
  3.  * Kind:     Parser specification (solution)
  4.  * Author:   Prof. Dr. Juergen Vollmer <vollmer@dhbw-karlsruhe.de>
  5.  * $Id: expr.pars.in,v 1.7 2010/04/26 10:53:44 vollmer Exp $
  6.  */
  7.  
  8. // Exercises:
  9. //  - Add computation of the "Value" Attribute to all grammar rules.
  10. //  - Test division by 0!
  11. //  - Add grammar and evaluation rules to compute "sin()", "cos()", and "tan()"
  12. //    see     man (3) sin
  13. //    Add a grammar and evaluation rule for the constant    "pi"
  14. //  - Add more test sources to test your grammar
  15.  
  16. SCANNER oberon_scan
  17.  
  18. PARSER  oberon_pars
  19.  
  20. GLOBAL {
  21.  # include <stdio.h>
  22.  # include <math.h>
  23. }
  24.  
  25. PREC LEFT  '~'
  26.      LEFT  '*' '/' 'DIV' 'MOD' '&'
  27.      LEFT  '+' '-' 'OR'
  28.      LEFT  '=' '#' '<' '<=' '>' '>=' 'IN' 'IS'
  29.  
  30. RULE
  31. root    = all.
  32.  
  33. identdef = <
  34.     = tok_identifier '*'
  35.     .
  36. > . /* identdef */
  37.  
  38. qualident = <
  39.     = tok_identifier '.' tok_identifier
  40.     .
  41.     = tok_identifier
  42.     .
  43. > . /* qualident */
  44.  
  45. expression = <
  46.     = '(' expression ')'
  47.     .
  48.     = expression '=' expression
  49.     .
  50.     = expression '#' expression
  51.     .
  52.     = expression '<' expression
  53.     .
  54.     = expression '<=' expression
  55.     .
  56.     = expression '>' expression
  57.     .
  58.     = expression '>=' expression
  59.     .
  60.     = expression 'IN' expression
  61.     .
  62.     = expression 'IS' expression
  63.     .
  64.     = expression '+' expression
  65.     .
  66.     = expression '-' expression
  67.     .
  68.     = expression 'OR' expression
  69.     .
  70.     = expression '*' expression
  71.     .
  72.     = expression '/' expression
  73.     .
  74.     = expression 'DIV' expression
  75.     .
  76.     = expression 'MOD' expression
  77.     .
  78.     = expression '&' expression
  79.     .
  80.     = number
  81.     .
  82.     = char_const
  83.     .
  84.     = string_const
  85.     .
  86.     = 'NIL'
  87.     .
  88.     = '~' expression
  89.     .
  90.     = set
  91.     .
  92. > . /* expression */
  93.  
  94. set = <
  95.     = '{' element_ls '}'
  96.     .
  97. > . /* set */
  98.  
  99. element_ls = <      /* <- Das ist eine sperarierte Liste */
  100.     = /* epsilon */
  101.     .
  102.     element_s = <
  103.         = element
  104.         .
  105.         = element_s ',' element
  106.         .
  107.     > . /* element_s */
  108. >. /* element_ls */
  109.  
  110. element = <
  111.     = expression
  112.     .
  113.     = expression '..' expression
  114.     .
  115. > . /* element */
  116.  
  117. number = <
  118.     = int_const
  119.     .
  120.     = real_const
  121.     .
  122. > . /* number */
  123.  
  124. /****************************************/
  125.  
  126. all = <
  127.     = int_const
  128.     .
  129.     = real_const
  130.     .
  131.     = char_const
  132.     .
  133.     = string_const
  134.     .
  135.     = identdef
  136.     .
  137.     = qualident
  138.     .
  139.     = expression
  140.     .
  141. > . /* all */
  142.  
  143. /* Tokens */
  144. int_const:      [Value: long]     {Value := 0;          } .
  145. real_const:     [Value: double]   {Value := 0.0;        } .
  146. char_const:     [Value: char]     {Value := '\0';       } .
  147. string_const:   [Value: tIdent]   {Value := NoIdent;    } .
  148. tok_identifier: [Value: tIdent]   {Value := NoIdent;    } .
  149.  
  150. /* non-terminal attributes */
  151. MODULE attributes
  152. PROPERTY SYN
  153. DECLARE
  154. END attributes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement