Advertisement
Mikescher

Cocktail [0] - All

Nov 26th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. ###############################################################################
  2. # Project: COCKTAIL training
  3. # Descr: LR parser for an oberonession language
  4. # Kind: Makefile
  5. # Author: Dr. Juergen Vollmer <Juergen.Vollmer@informatik-vollmer.de>
  6. # $Id: Makefile,v 1.5 2007/06/03 16:52:25 vollmer Exp $
  7. ###############################################################################
  8.  
  9. # The executable to produce:
  10. MAIN = oberon-parser
  11.  
  12. # Source files
  13. SRC_C = main.c
  14. SRC_H =
  15. SRC = oberon.scan oberon.pars
  16.  
  17. # Generated files
  18. GEN_C = oberon_scan.c oberon_scanSource.c oberon_pars.c
  19. GEN_H = oberon_scan.h oberon_scanSource.h oberon_pars.h
  20. GEN = oberon.rex oberon_scan.rpp oberon_pars.lrk
  21.  
  22. ###############################################################################
  23.  
  24. include ../config.mk
  25.  
  26. ###############################################################################
  27.  
  28. # Create your parser
  29. all: $(MAIN)
  30.  
  31. # Run the test suite
  32. test:
  33. ./$(MAIN) test1; echo
  34. ./$(MAIN) test2; echo
  35.  
  36. ###############################################################################
  37.  
  38. # Create the Parser
  39. oberon_pars.lrk oberon_scan.rpp: oberon$(SOLUTION).pars
  40. lpp -cxzj oberon$(SOLUTION).pars
  41.  
  42. LARK_OPTS = -cdi -Dw -5 # -5 ==> create the graphic LR-browser
  43. LARK_OPTS = -cdi -Dw # ==> see ../config.mk
  44. oberon_pars.c oberon_pars.h: oberon_pars.lrk
  45. lark $(LARK_OPTS) oberon_pars.lrk
  46.  
  47. # Create the Scanner
  48. oberon.rex: oberon.scan oberon_scan.rpp
  49. rpp < oberon.scan oberon_scan.rpp > oberon.rex
  50.  
  51. oberon_scan.c oberon_scan.h oberon_scanSource.c oberon_scanSource.h: oberon.rex
  52. rex -cdis -y oberon.rex
  53. rm -f oberon_scanDrv.c
  54.  
  55. ###############################################################################
  56.  
  57. include ../common.mk
  58.  
  59. ###############################################################################
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. SCANNER oberon_scan
  105.  
  106. EXPORT {
  107. /* code to be put intp Scanner.h */
  108. # include "Position.h"
  109.  
  110. /* Token Attributes.
  111. * For each token with user defined attributes, we need a typedef for the
  112. * token attributes.
  113. * LPP extracts the token-attribute declaration from the parser specification.
  114. * They are inserted here.
  115. */
  116. INSERT tScanAttribute
  117. }
  118.  
  119. GLOBAL {
  120. /* code to be put into Scanner.c */
  121. # include <stdlib.h>
  122. # include "Errors.h"
  123.  
  124. /* Insert the routine computing "error-values" of attributes, in case the
  125. * parser decides during error repair to insert a token.
  126. */
  127. INSERT ErrorAttribute
  128. }
  129.  
  130. LOCAL {
  131. /* user-defined local variables of the generated GetToken routine */
  132. # define MAX_STRING_LEN 2048
  133. char string[MAX_STRING_LEN];
  134. int nesting = 0;
  135. }
  136.  
  137. DEFAULT {
  138. /* What happens if no scanner rule matches the input */
  139. MessageI ("Illegal character",
  140. xxError, oberon_scan_Attribute.Position,
  141. xxCharacter, oberon_scan_TokenPtr);
  142. }
  143.  
  144. EOF {
  145. /* What should be done if the end-of-input-file has been reached? */
  146.  
  147. /* E.g.: check hat strings and comments are closed. */
  148. switch (yyStartState) {
  149. case STD:
  150. /* ok */
  151. break;
  152. default:
  153. Message ("OOPS: that should not happen!!",
  154. xxFatal, oberon_scan_Attribute.Position);
  155. break;
  156. }
  157.  
  158. /* implicit: return the EofToken */
  159. }
  160.  
  161. DEFINE /* some abbreviations */
  162. digit = {0-9} .
  163.  
  164. RULES
  165.  
  166. /* Integers */
  167. #STD# {0-9}+ :
  168. {oberon_scan_GetWord (string);
  169. oberon_scan_Attribute.int_const.Value = atol (string);
  170. return int_const;
  171. }
  172.  
  173. /* Float numbers */
  174. #STD# digit + "." digit * (("E"|"e") ("+"|"-") ? digit +) ? :
  175. {oberon_scan_GetWord (string);
  176. oberon_scan_Attribute.float_const.Value = atof (string);
  177. return float_const;
  178. }
  179.  
  180. INSERT RULES #STD#
  181.  
  182. /**********************************************************************/
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. /* Project: COCKTAIL training
  235. * Descr: A simple pocket computer (scanner, parser, evaluator)
  236. * Kind: Parser specification (solution)
  237. * Author: Prof. Dr. Juergen Vollmer <vollmer@dhbw-karlsruhe.de>
  238. * $Id: expr.pars.in,v 1.7 2010/04/26 10:53:44 vollmer Exp $
  239. */
  240.  
  241. // Exercises:
  242. // - Add computation of the "Value" Attribute to all grammar rules.
  243. // - Test division by 0!
  244. // - Add grammar and evaluation rules to compute "sin()", "cos()", and "tan()"
  245. // see man (3) sin
  246. // Add a grammar and evaluation rule for the constant "pi"
  247. // - Add more test sources to test your grammar
  248.  
  249. SCANNER oberon_scan
  250.  
  251. PARSER oberon_pars
  252.  
  253. GLOBAL {
  254. # include <stdio.h>
  255. # include <math.h>
  256. }
  257.  
  258.  
  259. RULE
  260. root = expr.
  261.  
  262. expr = <
  263. = int_const
  264. {Value := int_const:Value;}
  265. .
  266. = float_const
  267. {Value := float_const:Value;}
  268. .
  269. > . /* expr */
  270.  
  271. /* Tokens */
  272. int_const: [Value: long] {Value := 0; } .
  273. float_const: [Value: double] {Value := 0.0;} .
  274.  
  275. /* non-terminal attributes */
  276. MODULE attributes
  277. PROPERTY SYN
  278. DECLARE
  279. expr = [Value: double] .
  280. END attributes
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. /* Project: COCKTAIL training
  336. * Descr: LR parser for an oberon language
  337. * Kind: C-main program
  338. * Author: Dr. Juergen Vollmer <Juergen.Vollmer@informatik-vollmer.de>
  339. * $Id: main.c,v 1.2 2007/06/01 15:41:25 vollmer Exp $
  340. */
  341.  
  342. # include <stdio.h>
  343. # include <stdlib.h>
  344. # include <string.h>
  345. # include "Position.h"
  346. # include "Errors.h"
  347. # include "oberon_scan.h"
  348. # include "oberon_pars.h"
  349.  
  350. int main (int argc, char *argv[])
  351. {
  352. int errors = 0;
  353. if (argc == 2) {
  354. if (strcmp (argv[1], "-h") == 0) {
  355. fprintf (stderr,
  356. "usage: %s [-h] [file]\n"
  357. " oberonession LR based parser, reads `file' or stdin\n"
  358. " -h: Help\n", argv[0]);
  359. exit (0);
  360. }
  361.  
  362. oberon_scan_Attribute.Position.FileName = MakeIdent1 (argv[1]);
  363. oberon_scan_BeginFile (argv[1]);
  364. /* Read from file argv[1].
  365. * If this routine is not called, stdin is read.
  366. */
  367. }
  368.  
  369. errors = oberon_pars (); /* the parser */
  370.  
  371. printf ("parser returned: %d number of errors: %d\n",
  372. errors,GetCount (xxError));
  373.  
  374. return 0;
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement