|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectitec.minicompiler.Parser
The Parser
implements a top-down-parser for the PL0-language. It
uses the FIRST-Sets of the non-terminals and a lookahead Token, which is one
step ahead of the actual position, to check for errors. WEAK- and SYNC-points
are used for synchronization and a better resumption after errors. To get these
WEAK- and SYNC-points the FOLLOW-Sets of the non-terminals are used.
Field Summary | |
private int |
actualColumn
The line and column of the actualToken |
private int |
actualLine
The line and column of the actualToken |
private Token |
actualToken
The Token at the actual position of the Parser |
private java.util.HashSet |
CompareOperators
The Sets of compare-operators defined in the grammar |
private ErrorHandler |
errorhandler
The ErrorHandler used by the actual Parser |
private FirstSet |
firstset
The FIRST-Sets of the grammar |
private FollowSet |
followset
The FOLLOW-Sets of the grammar |
private java.util.HashSet |
LineOperators
The Set of line-operators defined in the grammar (+, -, OR) |
private int |
lookaheadColumn
The line and column of the lookaheadToken |
private int |
lookaheadLine
The line and column of the lookaheadToken |
private Token |
lookaheadToken
The Token at the next position of the Parser. |
private java.util.HashSet |
PointOperators
The Set of point-operators defined in the grammar (*, DIV, MOD, &) |
private Scanner |
scanner
The Scanner used by the actual Parser |
Constructor Summary | |
Parser(ErrorHandler e,
Scanner s)
Initialize the Parser with an ErrorHandler e and a Scanner
s . |
Method Summary | |
private void |
actualParameters()
Implements the grammar-rule:
ActualParameters -> Expression {"," Expression}
|
private void |
arrayType()
Implements the grammar-rule:
ArrayType -> "ARRAY" Expression "OF" Type
|
private void |
assignment()
Implements the grammar-rule:
Assignment -> Selector ":=" Expression
|
private void |
declarations()
Implements the grammar-rule:
Declarations -> ["CONST" {ident "=" Expression} ";"]
["TYPE" {ident "=" Type ";"}]
["VAR" Identlist ":" Type ";"]
{ProcedureDeclaration ";"}
|
private void |
error(int n)
Call the ErrorHandler with the error number n . |
private void |
expect(int kind,
int nonTerminal)
Check wether the next Token is the one which is needed or not. |
private void |
expectWeak(int kind)
Check wether the next Token is the one which is needed or not. |
private void |
expression()
Implements the grammar-rule:
Expression -> SimpleExpression [("="|"#"|"<"|"<="|">"|">=") SimpleExpression]
|
private void |
factor()
Implements the grammar-rule:
Factor -> ((ident Selector)|integer|("("Expression")")|("~" Factor))
|
private void |
fieldList()
Implements the grammar-rule:
FieldList -> IdentList ":" Type
|
private void |
formalParameters()
Implements the grammar-rule:
FormalParameters -> FPSection (";" FPSection}
|
private void |
fpSection()
Implements the grammar-rule:
FPSection -> "VAR" IdentList ":" Type
|
private void |
get()
Get the next Token from the Scanner. |
private void |
identList()
Implements the grammar-rule:
IdentList -> ident {"," ident}
|
private void |
ifStatement()
Implements the grammar-rule:
IfStatement -> "IF" Expression "THEN" StatementSequence
{"ELSIF" Expression "THEN" StatementSequence}
["ELSE" StatementSequence] "END"
|
private boolean |
isInFirst(int nonTerminal)
Return wether the lookaheadToken is in the FIRST-Set of
nonTerminal |
void |
parse()
Parse the whole source code |
private void |
pl0()
Implements the grammer-rule:
Pl0 -> "MODULE" ident ";" Declarations ["BEGIN" StatementSequence]
"END" ident "."
|
private void |
procedureBody()
Implements the grammar-rule:
ProcedureBody -> Declarations ["BEGIN" StatementSequence] "END"
|
private void |
procedureCall()
Implements the grammar-rule:
ProcedureCall -> "(" [ActualParameters] ")"
|
private void |
procedureDeclaration()
Implements the grammar-rule:
ProcedureDeclaration -> ProcedureHeading ";" ProcedureBody ident
|
private void |
procedureHeading()
Implements the grammar-rule:
ProcedureHeading -> "PROCEDURE" ident "(" [FormalParameters] ")"
|
private void |
recordType()
Implements the grammar-rule:
RecordType -> "RECORD" FieldList {";" FieldList} "END"
|
private void |
repeatStatement()
Implements the grammar-rule:
RepeatStaement -> "REPEAT" StatementSequence "UNTIL" Expression
|
private void |
selector()
Implements the grammar-rule:
Selector -> {("." ident)|("[" Expression "]")}
|
private void |
simpleExpression()
Implements the grammar-rule:
SimpleExpression -> ["+"|"-"] Term {("+"|"-"|"OR") Term}
|
private void |
statement()
Implements the grammar-rule:
Statement -> [(ident(Assignment|ProcedureCall))
|IfStatement
|WhileStatement
|RepeatStatement]
|
private void |
statementSequence()
Implements the grammar-rule:
StatementSequence -> Statement {";" Statement}
|
private void |
term()
Implements the grammar-rule:
Term -> Factor {("*"|"DIV"|"MOD"|"&") Factor}
|
private void |
type()
Implements the grammar-rule:
Type -> (ident|ArrayType|RecordType)
|
private boolean |
weakSeparator(int kind,
int nonTerminal)
Check wether a seperator (i.e , or ; ) is present.
|
private void |
whileStatement()
Implements the grammar-rule:
WhileStatement -> "WHILE" Expression "DO" StatementSequence "END"
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private FirstSet firstset
private FollowSet followset
private java.util.HashSet CompareOperators
private java.util.HashSet LineOperators
private java.util.HashSet PointOperators
private ErrorHandler errorhandler
private Scanner scanner
private Token actualToken
private Token lookaheadToken
private int actualLine
actualToken
private int actualColumn
actualToken
private int lookaheadLine
lookaheadToken
private int lookaheadColumn
lookaheadToken
Constructor Detail |
public Parser(ErrorHandler e, Scanner s)
e
and a Scanner
s
. Furthermore initialize the FirstSet and FollowSet and the
sets of compare-, line- and point-operators.
e
- The ErrorHandler the Parser should uses
- The Scanner the Parser gets it's information fromMethod Detail |
private void error(int n)
n
.
n
- The error number of the error which occuredprivate void get()
private void expect(int kind, int nonTerminal)
nonTerminal
.
kind
- The kind of the expected TokennonTerminal
- The non-terminal for which a SYNC-point should be reachedprivate void expectWeak(int kind)
kind
- The kind of the expected Tokenprivate boolean weakSeparator(int kind, int nonTerminal)
,
or ;
) is present.
If yes, just read along. Otherwise check if the seperator is really needed
or if the statement is finished anyway.
kind
- The kind of the expected TokennonTerminal
- The non-terminal
lookaheadToken
is not the expected one,
False otherwiseprivate boolean isInFirst(int nonTerminal)
lookaheadToken
is in the FIRST-Set of
nonTerminal
nonTerminal
- The non-terminal for the check
lookaheadToken
is in the
FIRST-Set of nonTerminal
, False
otherwisepublic void parse()
private void pl0()
Pl0 -> "MODULE" ident ";" Declarations ["BEGIN" StatementSequence]
"END" ident "."
private void declarations()
Declarations -> ["CONST" {ident "=" Expression} ";"]
["TYPE" {ident "=" Type ";"}]
["VAR" Identlist ":" Type ";"]
{ProcedureDeclaration ";"}
private void type()
Type -> (ident|ArrayType|RecordType)
private void arrayType()
ArrayType -> "ARRAY" Expression "OF" Type
private void recordType()
RecordType -> "RECORD" FieldList {";" FieldList} "END"
private void fieldList()
FieldList -> IdentList ":" Type
private void identList()
IdentList -> ident {"," ident}
private void procedureDeclaration()
ProcedureDeclaration -> ProcedureHeading ";" ProcedureBody ident
private void procedureHeading()
ProcedureHeading -> "PROCEDURE" ident "(" [FormalParameters] ")"
private void procedureBody()
ProcedureBody -> Declarations ["BEGIN" StatementSequence] "END"
private void formalParameters()
FormalParameters -> FPSection (";" FPSection}
private void fpSection()
FPSection -> "VAR" IdentList ":" Type
private void statementSequence()
StatementSequence -> Statement {";" Statement}
private void statement()
Statement -> [(ident(Assignment|ProcedureCall))
|IfStatement
|WhileStatement
|RepeatStatement]
private void assignment()
Assignment -> Selector ":=" Expression
private void selector()
Selector -> {("." ident)|("[" Expression "]")}
private void procedureCall()
ProcedureCall -> "(" [ActualParameters] ")"
private void actualParameters()
ActualParameters -> Expression {"," Expression}
private void ifStatement()
IfStatement -> "IF" Expression "THEN" StatementSequence
{"ELSIF" Expression "THEN" StatementSequence}
["ELSE" StatementSequence] "END"
private void whileStatement()
WhileStatement -> "WHILE" Expression "DO" StatementSequence "END"
private void repeatStatement()
RepeatStaement -> "REPEAT" StatementSequence "UNTIL" Expression
private void expression()
Expression -> SimpleExpression [("="|"#"|"<"|"<="|">"|">=") SimpleExpression]
private void simpleExpression()
SimpleExpression -> ["+"|"-"] Term {("+"|"-"|"OR") Term}
private void term()
Term -> Factor {("*"|"DIV"|"MOD"|"&") Factor}
private void factor()
Factor -> ((ident Selector)|integer|("("Expression")")|("~" Factor))
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |