Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################################################################
- # Project: COCKTAIL training
- # Descr: LR parser for an oberonession language
- # Kind: Makefile
- # Author: Dr. Juergen Vollmer <Juergen.Vollmer@informatik-vollmer.de>
- # $Id: Makefile,v 1.5 2007/06/03 16:52:25 vollmer Exp $
- ###############################################################################
- # The executable to produce:
- MAIN = oberon-parser
- # Source files
- SRC_C = main.c
- SRC_H =
- SRC = oberon.scan oberon.pars
- # Generated files
- GEN_C = oberon_scan.c oberon_scanSource.c oberon_pars.c
- GEN_H = oberon_scan.h oberon_scanSource.h oberon_pars.h
- GEN = oberon.rex oberon_scan.rpp oberon_pars.lrk
- ###############################################################################
- include ../config.mk
- ###############################################################################
- # Create your parser
- all: $(MAIN)
- # Run the test suite
- test:
- ./$(MAIN) test1; echo
- ./$(MAIN) test2; echo
- ###############################################################################
- # Create the Parser
- oberon_pars.lrk oberon_scan.rpp: oberon$(SOLUTION).pars
- lpp -cxzj oberon$(SOLUTION).pars
- LARK_OPTS = -cdi -Dw -5 # -5 ==> create the graphic LR-browser
- LARK_OPTS = -cdi -Dw # ==> see ../config.mk
- oberon_pars.c oberon_pars.h: oberon_pars.lrk
- lark $(LARK_OPTS) oberon_pars.lrk
- # Create the Scanner
- oberon.rex: oberon.scan oberon_scan.rpp
- rpp < oberon.scan oberon_scan.rpp > oberon.rex
- oberon_scan.c oberon_scan.h oberon_scanSource.c oberon_scanSource.h: oberon.rex
- rex -cdis -y oberon.rex
- rm -f oberon_scanDrv.c
- ###############################################################################
- include ../common.mk
- ###############################################################################
- SCANNER oberon_scan
- EXPORT {
- /* code to be put intp Scanner.h */
- # include "Position.h"
- /* Token Attributes.
- * For each token with user defined attributes, we need a typedef for the
- * token attributes.
- * LPP extracts the token-attribute declaration from the parser specification.
- * They are inserted here.
- */
- INSERT tScanAttribute
- }
- GLOBAL {
- /* code to be put into Scanner.c */
- # include <stdlib.h>
- # include "Errors.h"
- /* Insert the routine computing "error-values" of attributes, in case the
- * parser decides during error repair to insert a token.
- */
- INSERT ErrorAttribute
- }
- LOCAL {
- /* user-defined local variables of the generated GetToken routine */
- # define MAX_STRING_LEN 2048
- char string[MAX_STRING_LEN];
- int nesting = 0;
- }
- DEFAULT {
- /* What happens if no scanner rule matches the input */
- MessageI ("Illegal character",
- xxError, oberon_scan_Attribute.Position,
- xxCharacter, oberon_scan_TokenPtr);
- }
- EOF {
- /* What should be done if the end-of-input-file has been reached? */
- /* E.g.: check hat strings and comments are closed. */
- switch (yyStartState) {
- case STD:
- /* ok */
- break;
- default:
- Message ("OOPS: that should not happen!!",
- xxFatal, oberon_scan_Attribute.Position);
- break;
- }
- /* implicit: return the EofToken */
- }
- DEFINE /* some abbreviations */
- digit = {0-9} .
- RULES
- /* Integers */
- #STD# {0-9}+ :
- {oberon_scan_GetWord (string);
- oberon_scan_Attribute.int_const.Value = atol (string);
- return int_const;
- }
- /* Float numbers */
- #STD# digit + "." digit * (("E"|"e") ("+"|"-") ? digit +) ? :
- {oberon_scan_GetWord (string);
- oberon_scan_Attribute.float_const.Value = atof (string);
- return float_const;
- }
- INSERT RULES #STD#
- /**********************************************************************/
- /* Project: COCKTAIL training
- * Descr: A simple pocket computer (scanner, parser, evaluator)
- * Kind: Parser specification (solution)
- * Author: Prof. Dr. Juergen Vollmer <vollmer@dhbw-karlsruhe.de>
- * $Id: expr.pars.in,v 1.7 2010/04/26 10:53:44 vollmer Exp $
- */
- // Exercises:
- // - Add computation of the "Value" Attribute to all grammar rules.
- // - Test division by 0!
- // - Add grammar and evaluation rules to compute "sin()", "cos()", and "tan()"
- // see man (3) sin
- // Add a grammar and evaluation rule for the constant "pi"
- // - Add more test sources to test your grammar
- SCANNER oberon_scan
- PARSER oberon_pars
- GLOBAL {
- # include <stdio.h>
- # include <math.h>
- }
- RULE
- root = expr.
- expr = <
- = int_const
- {Value := int_const:Value;}
- .
- = float_const
- {Value := float_const:Value;}
- .
- > . /* expr */
- /* Tokens */
- int_const: [Value: long] {Value := 0; } .
- float_const: [Value: double] {Value := 0.0;} .
- /* non-terminal attributes */
- MODULE attributes
- PROPERTY SYN
- DECLARE
- expr = [Value: double] .
- END attributes
- /* Project: COCKTAIL training
- * Descr: LR parser for an oberon language
- * Kind: C-main program
- * Author: Dr. Juergen Vollmer <Juergen.Vollmer@informatik-vollmer.de>
- * $Id: main.c,v 1.2 2007/06/01 15:41:25 vollmer Exp $
- */
- # include <stdio.h>
- # include <stdlib.h>
- # include <string.h>
- # include "Position.h"
- # include "Errors.h"
- # include "oberon_scan.h"
- # include "oberon_pars.h"
- int main (int argc, char *argv[])
- {
- int errors = 0;
- if (argc == 2) {
- if (strcmp (argv[1], "-h") == 0) {
- fprintf (stderr,
- "usage: %s [-h] [file]\n"
- " oberonession LR based parser, reads `file' or stdin\n"
- " -h: Help\n", argv[0]);
- exit (0);
- }
- oberon_scan_Attribute.Position.FileName = MakeIdent1 (argv[1]);
- oberon_scan_BeginFile (argv[1]);
- /* Read from file argv[1].
- * If this routine is not called, stdin is read.
- */
- }
- errors = oberon_pars (); /* the parser */
- printf ("parser returned: %d number of errors: %d\n",
- errors,GetCount (xxError));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement