NAME: ____________________________________________ ID: ____________ CS444 grammar lesson nov 14th: Prompt for this page: show tokens (numbered, one line per each listing basic type (keyword, identifier, constant, string literal, operator, other separator), Token_type and lexeme, C grammar derivation and abstract syntax tree for simplified nov12.c which is: int main(int argc, char *argv[], char *envp[]){ int k; k = 0; while (envp[k] != 0) { k++; } return 0; } Answer: lexical analysis (tokens), C grammar derivation, abstract syntax tree for your C program. 1. Lexical Analysis (Tokens) 44 tokens according to standard C lexical categories # Basic Type Token_type Lexeme 1 Keyword INT int 2 Identifier IDENTIFIER main 3 Other separator LPAREN ( 4 Keyword INT int 5 Identifier IDENTIFIER argc 6 Other separator COMMA , 7 Keyword CHAR char 8 Operator ASTERISK (or STAR) * 9 Identifier IDENTIFIER argv 10 Other separator LBRACKET [ 11 Other separator RBRACKET ] 12 Other separator COMMA , 13 Keyword CHAR char 14 Operator ASTERISK (or STAR) * 15 Identifier IDENTIFIER envp 16 Other separator LBRACKET [ 17 Other separator RBRACKET ] 18 Other separator RPAREN ) 19 Other separator LBRACE { 20 Keyword INT int 21 Identifier IDENTIFIER k 22 Other separator SEMICOLON ; 23 Identifier IDENTIFIER k 24 Operator ASSIGN = 25 Constant INT_LITERAL 0 26 Other separator SEMICOLON ; 27 Keyword WHILE while 28 Other separator LPAREN ( 29 Identifier IDENTIFIER envp 30 Other separator LBRACKET [ 31 Identifier IDENTIFIER k 32 Other separator RBRACKET ] 33 Operator NEQ != 34 Constant INT_LITERAL 0 35 Other separator RPAREN ) 36 Other separator LBRACE { 37 Identifier IDENTIFIER k 38 Operator INC ++ 39 Other separator SEMICOLON ; 40 Other separator RBRACE } 41 Keyword RETURN return 42 Constant INT_LITERAL 0 43 Other separator SEMICOLON ; 44 Other separator RBRACE } 2. C Grammar Derivation (Leftmost) Condensed leftmost derivation from to the terminal tokens: => => => => int => int ( ) => int main ( ) => int main ( , , ) => int main ( int argc , char *argv[] , char *envp[] ) { } => int main ( int argc , char *argv[] , char *envp[] ) { } --- Expanding (int k;) --- => int ; => int k ; --- Expanding 1st (k = 0;) --- => => ; => = ; => k = 0 ; --- Expanding 2nd (while (envp[k] != 0) { k++; }) --- => => while ( ) => while ( ) => while ( != ) { } => while ( envp [ k ] != 0 ) { } => while ( envp [ k ] != 0 ) { ++ ; } => while ( envp [ k ] != 0 ) { k ++ ; } --- Expanding 3rd (return 0;) --- => => return ; => return 0 ; 3. Abstract Syntax Tree (AST) Unlike a parse tree, the AST strips away syntactic punctuation (;, (, ), {, }) and represents the hierarchical structure of the program's execution and declarations. text TranslationUnit └── FunctionDecl: main (return_type: int) ├── Parameters │ ├── ParamDecl: argc (type: int) │ ├── ParamDecl: argv (type: char*[]) │ └── ParamDecl: envp (type: char*[]) └── CompoundStmt (Function Body) ├── VarDecl: k (type: int) ├── BinaryOperator: '=' (Assignment) │ ├── DeclRefExpr: k │ └── IntegerLiteral: 0 ├── WhileStmt │ ├── Condition: BinaryOperator '!=' │ │ ├── ArraySubscriptExpr │ │ │ ├── DeclRefExpr: envp │ │ │ └── DeclRefExpr: k │ │ └── IntegerLiteral: 0 │ └── Body: CompoundStmt │ └── UnaryOperator: '++' (Postfix) │ └── DeclRefExpr: k └── ReturnStmt └── IntegerLiteral: 0