Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * parser.cup -- SPL parser specification
- */
- package parse;
- import java_cup.runtime.*;
- parser code {:
- /* Erweiterung der Parser-Klasse */
- public void syntax_error(Symbol currToken) {
- System.out.println(
- "**** Error: syntax error" +
- " in line " + currToken.left +
- ", column " + currToken.right
- );
- System.exit(0);
- }
- :}
- /* Definition der Terminalsymbole */
- terminal ARRAY, ELSE, IF, OF, PROC, REF, TYPE, VAR, WHILE, LCURL, RCURL;
- terminal LPAREN, RPAREN, LBRACK, RBRACK;
- terminal EQ, NE, LT, LE, GT, GE, ASGN, COLON, COMMA, SEMIC;
- terminal PLUS, MINUS, STAR, SLASH;
- terminal String IDENT;
- terminal Integer INTLIT;
- /* Definition der Nonterminalsymbole */
- non terminal program;
- non terminal DecList GlobalDeclarations; //Rückgabetyp DecList
- non terminal Dec GlobalDec, TypeDec, paramlist, ProcDec; //Rckgabetyp Dec
- non terminal VarDec SimpleVar, ArrayVar; //Rückgabetyp VarDec
- non terminal param, typedec;
- non terminal GlobalDeclaration;
- non terminal primaryexp, instruction;
- non terminal expression, term, factor;
- /*An expression is made of a sum of terms and a term is made of a product of factors and a factor is either a number or a parenthesized subexpression.*/
- start with program;
- /*DecList repräsentiert eine Liste von Deklarationen.
- Für die Listenelemente gibt es verschiedene Varianten, z.B.Typdeklarationen (TypeDec), Prozedurdeklarationen (ProcDec) oder Variablendeklarationen (VarDec).
- ProcDec repräsentiert eine Prozedurdeklaration.
- Der Baumknoten hat ein Attribut vom Typ Sym, das den Prodedur-bezeichner ”aux“ enthält, und drei komplexe Unterstrukturen, die durch Kindknoten repräsentiert werden:
- –Die Liste der Parameterdeklarationen: Ein DecList-Knoten
- –Die Liste der Deklarationen lokaler Variablen: Ebenfalls ein DecList-Knoten
- –Der Prozedur-Rumpf: Ein StmList-Knoten, der die Sequenz von Anweisungen (”Statement List“) repräsentiert,aus denen der Rumpf besteht.
- Was es mit der Klasse Sym auf sich hat, die nicht nur für Prodedurbezeichner, sondern für Bezeichner aller Art wird,ist weiter unten erläutert.
- •SimpleVar und ArrayVar sind Referenzen, also Speicherplatzrepräsentanten, die an bestimmten Stellen implizit dereferenziert werden. Im AST wird eine Dereferenzierung durch einen VarExp-Knoten dargestellt*/
- /* Hier beginnen der Hauptteil: Die Ableitungsregeln */
- program ::= GlobalDeclarations;
- typedec ::= TYPE IDENT EQ type SEMIC;
- type ::= IDENT | ARRAY LBRACK INTLIT RBRACK OF type;
- proc ::= PROC IDENT LPAREN paramList RPAREN LCURL decList instructList RCURL;
- decList ::= var_declaration | /*epsilon*/;
- var_declaration ::= VAR IDENT COLON type SEMIC;
- paramList ::= neparamlist| /*epsilon*/;
- neparamList ::= param COMMA neparamlist | param;
- param ::= IDENT COLON Type | REF IDENT COLON type;
- instructList ::= instruction instructlist | /*epsilon*/; /*Head Tail*/
- instruction ::= WhileStatement | IfStatement | SEMIC | assignment;
- lhs ::= IDENT | lhs LBRACK Exp RBRACK ;
- assignment ::= lhs ASGN Exp SEMIC;
- proccall
- WhileStatement ::= WHILE LPAREN Exp RPAREN Statement;
- IfStatement ::= IF LPAREN Exp RPAREN Statement
- |IF LPAREN Exp RPAREN instruction ELSE instruction;
- instruction
- Exp ::= compareexp;
- compareexp ::= expression EQ expression
- | expression NE expression
- | expression LT expression
- | expression LE expression
- | expression GT expression
- | expression GE expression
- | expression;
- expression ::= expression PLUS term
- | expression MINUS term
- | term;
- term ::= term STAR faktor
- | term SLASH faktor
- | faktor;
- factor ::= MINUS primaryexp | primaryexp;
- primaryexp ::= INTLIT | LPAREN Exp RPAREN | lhs;
- //Alle Listen können leer sein
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement