04.Top-Down - http/usecurity.hanyang.ac.kr Chapter 4...

This preview shows page 1 - 13 out of 58 pages.

Chapter 4 Top - Down Parsing 한양대학교 컴퓨터공학부 컴파일러 2014 2 학기
Image of page 1
Top-down parsing Definition Parsing an input string of tokens by tracing out the steps in a leftmost derivation. Categories Backtracking parsers Powerful but slow Predictive parsers Using one or more lookahead tokens Recursive-descent parsing LL(1) parsing. Top - down parsing 2
Image of page 2
Recursive descent parsing Each procedure is generated for each grammar rule. Expression grammar exp exp addop term | term addop + | - term term mulop factor | factor mulop * factor ( exp ) | number Recursive - descent parsing 3 5 procedures are required.
Image of page 3
factor ( exp ) | number terminal: match () nonterminal: function procedure factor begin case token of (: match ( ( ) ; exp ; match ( ) ) ; number : match ( number ) ; else error ; end case ; end factor ; Recursive - descent parsing 4
Image of page 4
Recursive - descent parsing procedure match( expectedToken) ; begin if token = expectedToken then getToken ; else error ; end if ; end match ; compare match ( ( ) with match ( ) ) in procedure factor . 5
Image of page 5
if -stmt if ( exp ) statement | if ( exp ) statement else statement EBNF if-stmt if ( exp ) statement [ else statement ] Recursive - descent parsing procedure ifStmt ; begin match ( if ) ; match ( ( ) ; exp ; match ( ) ) ; statement ; if token = else then match ( else ) ; statement ; end if ; end ifStmt ; 6
Image of page 6
Recursive - descent parsing procedure exp ; begin case token of ?: exp ; addop ; term ; ?: term ; end case end exp ; exp exp addop term | term 7
Image of page 7
Recursive - descent parsing procedure exp ; begin term ; while token = + or token = - do match ( token ) ; term ; end while ; end exp ; exp exp addop term | term EBNF exp term { addop term } 8
Image of page 8
Recursive - descent parsing procedure term ; begin factor ; while token = * do match ( token ) ; factor ; end while ; end term ; term term mulop factor | factor EBNF term factor { mulop factor } 9
Image of page 9
Recursive - descent parsing 10 function exp : integer ; var temp : integer ; begin temp := term ; while token = + or token = - do case token of + : match ( + ) ; temp := temp + term ; - : match ( - ) ; temp := temp term ; end case ; end while ; return temp ; end exp ; Left associativity is conserved. A simple integer arithmetic exp term { addop term } A simple calculator p. 148-149
Image of page 10
Recursive - descent parsing function exp : syntaxTree ; var temp, newtemp : syntaxTree ; begin temp := term ; while token = + or token = - do case token of + : match ( + ) ; newtemp : = makeOpNode ( + ) ; leftChild ( newtemp ) := temp ; rightChild ( newtemp ) := term ; temp := newtemp ; - : match ( - ) ; newtemp := makeOpNode ( - ) ; leftChild ( newtemp ) := temp ; rightChild ( newtemp ) := term ; temp := newtemp ; end case ; end while ; return temp ; end exp ; Syntax tree generation 11
Image of page 11
Recursive - descent parsing function exp : syntaxTree ; var temp, newtemp : syntaxTree ; begin temp := term ; while token = + or token = - do newtemp : = makeOpNode ( token ) ; match ( token ) ;
Image of page 12

Want to read all 58 pages?

Image of page 13

Want to read all 58 pages?

You've reached the end of your free preview.

Want to read all 58 pages?

  • Spring '16
  • ko
  • Formal grammar, LL, Recursive descent parser

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern