02simple - A SIMPLE COMPILER SIMPLE Chuen-Liang Chen...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
A SIMPLE COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering ational Taiwan University National Taiwan University Taipei, TAIWAN
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
tructures of compilers /3) Structures of compilers (2/3) calling tree (1 pass) main pass 1 parser scanner semantic utines optimizer token SS code enerator achine routines ymbol table ource generator machine code symbol table attribute table SS : syntactic structure (parse tree) source code
Background image of page 2
anguage specification Language specification grammar 1. <program> begin <statement list> end statement list> statement> statement> 2. <statement list> <statement> { <statement> } 3. <statement> ID := <expression> ; 4. <statement> read ( <id list> ) ; . statement> rite <expr list> ) ; 5. <statement> write ( <expr list> ) ; 6. <id list> ID { , ID } 7. <expr list> <expression> { , <expression> } 8. <expression> <primary> { <add op> <primary> } p py { pp y 9. <primary> ( <expression> ) 10. <primary> ID 11. <primary> INTLITERAL 12. <add op> + 13. <add op> - 14. <system goal> <program> SCANEOF Backus-Naur form ( BNF ) ID letter { letter | digit | underline } * TLITERAL igit digit * INTLITERAL digit digit comment - - anything EOL
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
okens Tokens sequence of characters having a collective meaning xample example 1. <program> begin <statement list> end 2. <statement list> <statement> { <statement> } 3. <statement> ID := <expression> ; 4. <statement> read ( <id list> ) ; 5. <statement> write ( <expr list> ) ; 6. <id list> ID { , ID } 7. <expr list> <expression> { , <expression> } . <expression> <primary> { <add op> <primary> } 8 p py { p 9. <primary> ( <expression> ) 10. <primary> ID 1 primary> TLITERAL 11. <primary> INTLITERAL 12. <add op> + 13. <add op> - 4 system goal> program> CANEOF 14. <system goal> <program> SCANEOF
Background image of page 4
canner /3) Scanner (1/3) called by parser, usually to group input characters into tokens pe of tokens egin nd ad rite entifier integer ( ) ; + type of tokens -- begin end read write identifier integer ( ) ; , + - := excluding -- comment, blank, tab, . .. QUIZ: benefit ? including -- End-Of-File QUIZ: if exclude EOF, then . . . ? ey issues key issues do not read too many how to distinguish different identifiers (integers) ? ow to recognize egin nd ad rite om identifiers ? how to recognize begin end read write from identifiers ? comments ungetc() -- for lookahead buffer_char() -- save in_char into token buffer check_reserved() -- check whether token in buffer is a reserved word & return BEGIN , END , READ , WRITE , or ID ( token code ) BEGIN, END, READ, WRITE and ID are integer constants, usually
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
canner /3) Scanner (2/3) #include <stdio.h> * character classification macros */ / character classification macros / #include <ctype.h> extern char token_buffer[ ]; token scanner(void) else if ( isalpha(in_char) ) { /* { int in_char, c; clear_buffer(); (feof(stdin)) * ID ::= LETTER | ID LETTER *| I D D I G I T * | ID UNDERSCORE * if (feof(stdin)) return SCANEOF; while ((in_char = getchar()) != EOF) { if (isspace(in_char)) / buffer_char(in_char); for (c = getchar(); isalnum(c) || c == '_'; c = getchar()) continue; /* do nothing */ else if ( ??? ) { ???
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

02simple - A SIMPLE COMPILER SIMPLE Chuen-Liang Chen...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online