Skip to content

Parser

Latest
Compare
Choose a tag to compare
@MuttonYung MuttonYung released this 10 Oct 03:55
· 1 commit to main since this release

Parser works by building a Parse Tree, with each single leaf node as a terminator and non-leaf node as a syntactic constituent.

Parser Test

Carry out lexical and syntactic analysis.
Output each symbol's token and itself, and syntactic constituents except <BlockItem>, <Decl> and <BType>.

Example

testfile.txt

int main(){
    int c;
    c= getint();
    printf("%d",c);
    return c;
}

output.txt

INTTK int
MAINTK main
LPARENT (
RPARENT )
LBRACE {
INTTK int
IDENFR c
<VarDef>
SEMICN ;
<VarDecl>
IDENFR c
<LVal>
ASSIGN =
GETINTTK getint
LPARENT (
RPARENT )
SEMICN ;
<Stmt>
PRINTFTK printf
LPARENT (
STRCON "%d"
COMMA ,
IDENFR c
<LVal>
<PrimaryExp>
<UnaryExp>
<MulExp>
<AddExp>
<Exp>
RPARENT )
SEMICN ;
<Stmt>
RETURNTK return
IDENFR c
<LVal>
<PrimaryExp>
<UnaryExp>
<MulExp>
<AddExp>
<Exp>
SEMICN ;
<Stmt>
RBRACE }
<Block>
<MainFuncDef>
<CompUnit>