Skip to content
andreas-eberle edited this page Nov 6, 2014 · 33 revisions

Grammatik

Die Grammatik ist wegen mancher linksrekursiver Produktionen für kein k LL(k).

In jedem Fall muss in diesen Produktionen die linksrekursion für den rekursiven Abstieg eliminiert werden (vermutlich auch noch andere Produktionen):

  • LogicalOrExpression -> (LogicalOrExpression ||)? LogicalAndExpression
  • LogicalAndExpression -> (LogicalAndExpression &&)? EqualityExpression
  • EqualityExpression -> (EqualityExpression (== | !=))? RelationalExpression
  • RelationalExpression -> (RelationalExpression (< | <= | > | >=))? AdditiveExpression
  • AdditiveExpression -> (AdditiveExpression (+ | -))? MultiplicativeExpression
  • MultiplicativeExpression -> (MultiplicativeExpression (* | / | %))? UnaryExpression

This part will be done with Precedience Climbing.

Geänderte Produktionen (work in progress)

Um von SLL(3) zu SLL(2) zu kommen. Weniger sollte nicht gehen, da BasicType auch IDENT sein kann in NewExpression:

  • PrimaryExpression -> null | false | true | INTEGER_LITERAL | PrimaryIdent | this | ( Expression ) | new NewExpression
  • PrimaryIdent -> IDENT | IDENT ( Arguments )
  • NewExpression -> IDENT () | BasicType NewArrayExpression
  • NewArrayExpression -> [Expression] ([])*
  • PostfixOp -> MethodInvocationFieldAccess | ArrayAccess
  • MethodInvocationFieldAccess -> . IDENT MethodInvocationFieldAccess'
  • MethodInvocationFieldAccess' -> ( Arguments ) | epsilon

parse-Funktionen (wip)

Ich denke eine Gegenüberstellung von Produktionen, wie sie im Sprachbericht aufgeführt werden und den Produktionen, wie wir sie im Parser modofiziert implementieren, ist sinnvoll.

parseProgram:

  • Program -> ClassDeclaration*
  • ClassDeclaration -> class IDENT { ClassMember* }

geändert in

  • Program -> epislon | class IDENT { ClassMember' } Program

parseClassMember:

  • ClassMember -> Field | Method | MainMethod
  • Field -> public Type IDENT ;
  • MainMethod -> public static void IDENT ( String [ ] IDENT ) Block
  • Method -> public Type IDENT ( Parameters? ) Block

geändert in

  • ClassMember' -> public ClassMember''
  • ClassMember'' -> MainMethod' | Member
  • MainMethod' -> static void IDENT ( String [ ] IDENT ) Block
  • Member -> Type IDENT Member'
  • Member' -> ; | ( Parameters? ) Block

parseParameters:

  • Parameters -> Parameter (, Parameters)?

parseParameter:

  • Parameter -> Type IDENT

parseType:

  • Type -> Type [] | BasicType
  • BasicType -> int | boolean | void | IDENT

geändert in

  • Type -> Basictype ([])?
  • BasicType -> int | boolean | void | IDENT

parseStatement:

  • Statement -> Block | EmptyStatement | IfStatement | ExpressionStatement | WhileStatement | ReturnStatement

parseBlock:

  • Block -> { BlockStatement* }

parseBlockStatement:

  • BlockStatement -> Statement | LocalVariableDeclarationStatement
  • Statement -> Block | EmptyStatement | IfStatement | ExpressionStatement | WhileStatement | ReturnStatement

geändert in

  • BlockStatement -> Block | EmptyStatement | IfStatement | ExpressionStatement | WhileStatement | ReturnStatement | LocalVariableDeclarationStatement

parseLocalVariableDeclarationStatement:

  • LocalVariableDeclarationStatement -> Type IDENT (= Expression)? ;

parseEmptyStatement:

  • EmptyStatement -> ;

parseWhileStatement:

  • WhileStatement-> while ( Expression ) Statement

parseIfStatement:

  • IfStatement -> if ( Expression ) Statement (else Statement)?

parseExpressionStatement:

  • ExpressionStatement -> Expression ;

parseReturnStatement:

  • ReturnStatement -> return Expression? ;

Resultierende Grammatik

  • Program -> epislon | class IDENT { ClassMember' } Program * `

  • ClassMember' -> public ClassMember''

  • ClassMember'' -> MainMethod' | Member

  • MainMethod' -> static void IDENT ( String [ ] IDENT ) Block

  • Member -> Type IDENT Member'

  • Member' -> ; | ( Parameters? ) Block

  • Parameters -> Parameter (, Parameters)?

  • Parameter -> Type IDENT

  • Type -> Basictype ([])*

  • BasicType -> int | boolean | void | IDENT

  • Statement -> Block | EmptyStatement | IfStatement | ExpressionStatement | WhileStatement | ReturnStatement

  • Block -> { BlockStatement* }

  • BlockStatement -> Block | EmptyStatement | IfStatement | ExpressionStatement | WhileStatement | ReturnStatement | LocalVariableDeclarationStatement <-- SLL(3)

  • LocalVariableDeclarationStatement -> Type IDENT (= Expression)? ;

  • EmptyStatement -> ;

  • WhileStatement-> while ( Expression ) Statement

  • IfStatement -> if ( Expression ) Statement (else Statement)?

  • ExpressionStatement -> Expression ;

  • ReturnStatement -> return Expression? ;

  • Precedence climbing for expressions

  • UnaryExpression -> PostfixExpression | (! | -) UnaryExpression

  • PostfixExpression -> PrimaryExpression (PostfixOp)*

  • PostfixOp -> MethodInvocationFieldAccess | ArrayAccess

  • MethodInvocationFieldAccess -> . IDENT MethodInvocationFieldAccess'

  • MethodInvocationFieldAccess' -> ( Arguments ) | epsilon

  • ArrayAccess -> [ Expression ]

  • Arguments -> (Expression (, Expression)*)?

  • PrimaryExpression -> null | false | true | INTEGER_LITERAL | PrimaryIdent | this | ( Expression ) | new NewExpression

  • PrimaryIdent -> IDENT | IDENT ( Arguments )

  • NewExpression -> IDENT NewIdentExpression | boolean NewArrayExpression | int NewArrayExpression

  • NewArrayExpression -> [Expression] ([])* <-- SLL(2)

  • NewIdentExpression -> () | NewArrayExpression

Clone this wiki locally