1 / 14

CPSC 325 - Compiler

CPSC 325 - Compiler. Tutorial 1 Scanner & Parser. Scanner and Parser. Input. Scanner. token. Grammar. Parser. Parser Tree (Syntax Tree). Jeremy – noun sees – verb the – determiner cute – adjective monkey – noun. Token – example 1 Jeremy sees the cute monkey.

alair
Download Presentation

CPSC 325 - Compiler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 325 - Compiler Tutorial 1 Scanner & Parser

  2. Scanner and Parser Input Scanner token Grammar Parser Parser Tree (Syntax Tree)

  3. Jeremy – noun sees – verb the – determiner cute – adjective monkey – noun Token – example 1Jeremy sees the cute monkey.

  4. int – type (reserve/key word) x – variable = – assign (reserve/key word) ( – left-prentices (reserve/key word) 3 – digit + – plus (reserve/key word) 21 – digit ) – right-prentices (reserve/key word) * – multiple (reserve/key word) 6 – digit ; – semi-colon/end (reserve/key word) Token – example 2int x = ( 3 + 21 ) * 6;

  5. Parsing • Top-Down Parsing • Bottom-up Parsing • Ambiguous Jeremy sees the cute monkeys sleeps int x = ( 3 + 21 ) * 6;

  6. Grammar Context Free Grammar (CFG)

  7. Simple Debug • Miss the end int x = 3 // “;” missing – add it in • Extra ending int x = 3&; // “&” is extra – remove it • Output the possible part x = 3 + // ??? – what happen here? (Type, second argument, semi-colon, etc.) Note: Some of them is impossible to debug. (For example: misspell, missing argument)

  8. sentence Verb phrase Noun phrase noun verb Prep. phrase I sit prep noun on you Practice • Parse the following: The big dog crush the small kid. double x = y + 3 / 2; // Syntax • Write the grammar for the following:

  9. Practice Parse and write the grammar ( 5 + 2 ) * 4 – ( 1 + 2 )

  10. Lex • Lex will unify the string which fit the patterns • Good for search through a program or a document • You can specify in C or C++ for what action should take when an input string been found.

  11. Lex - Structure • Declarations/Definitions %% • Rules/Production - Lex expression - white space - C statement (optional) %% • Additional Code/Subroutines

  12. Lex – statement Example • %% [+++]+.* ; Remove all of comment in Aldor code. - after lex expression, the C statement is empty. (So, takes no action)

  13. Lex – Basic operators • * - zero or more occurrences • . - “ANY” character • .* - matches any sequence • | - separator • + - one or more occurrences. (a+ :== aa*) • ? - zero or one of something. (b? :== (b+null) • [ ] - choice, so [12345]  (1|2|3|4|5) (Note: [*+] represent a choice between star and plus. They lost their specialty. • - - [a-zA-Z]  a to z and A to Z, all the letters. • \ - \* matches *, and \. Match period or decimal point.

  14. Lex – Additional Code • %% main() { yylex(); [any C statements] } - When you compile the Lex file, __.lex or __.l, it will generates lex.yy.c file, which define yylex(). - Type “man lex” in UNIX system for more information.

More Related