《英語(yǔ)語(yǔ)法描述》PPT課件.ppt

收藏

編號(hào):117257952    類型:共享資源    大?。?span id="27tn75s" class="font-tahoma">268.31KB    格式:PPT    上傳時(shí)間:2022-07-08
15
積分
關(guān) 鍵 詞:
英語(yǔ)語(yǔ)法描述 》PPT課件 英語(yǔ)語(yǔ)法介紹 PPT課件.ppt 英語(yǔ)語(yǔ)法課件ppt 英語(yǔ)語(yǔ)法介紹PPT課件 PPT課件ppt 介紹ppt課件.ppt
資源描述:
Parsing,The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules Report errors Build IR Automation: The process can be automated,Parsing,Check and verify syntax based on specified syntax rules Are regular expressions sufficient for describing syntax? Example 1: Infix expressions Example 2: Nested parentheses We use Context-Free Grammars (CFGs) to specify context-free syntax. A CFG describes how a sentence of a language may be generated. Example: Use this grammar to generate the sentence mwa ha ha ha!,EvilLaugh mwa EvilCackle EvilCackle ha EvilCackle EvilCackle ha!,CFGs,A CFG is a quadruple (N, T, R, S) where N is the set of non-terminal symbols T is the set of terminal symbols S N is the starting symbol R N(NT)* is a set of rules Example: The grammar of nested parentheses G = (N, T, R, S) where N = S T = (, ) R = S (S) , SSS, S ,Derivations,The language described by a CFG is the set of strings that can be derived from the start symbol using the rules of the grammar. At each step, we choose a non-terminal to replace.,S (S) (SS) (S)S) ( )S) ( )(S) ( )(S) ( )( ),derivation,sentential form,This example demonstrates a leftmost derivation : one where we always expand the leftmost non-terminal in the sentential form.,Derivations and parse trees,We can describe a derivation using a graphical representation called parse tree: the root is labeled with the start symbol, S each internal node is labeled with a non-terminal the children of an internal node A are the right-hand side of a production A each leaf is labeled with a terminal A parse tree has a unique leftmost and a unique rightmost derivation (however, we cannot tell which one was used by looking at the tree),Derivations and parse trees,So, how can we use the grammar described earlier to verify the syntax of “( )( )“? We must try to find a derivation for that string. We can work top-down (starting at the root/start symbol) or bottom-up (starting at the leaves). Careful! There may be more than one grammars to describe the same language. Not all grammars are suitable,Problems in parsing,Consider S if E then S else S | if E then S What is the parse tree for if E then if E then S else S There are two possible parse trees! This problem is called ambiguity A CFG is ambiguous if one or more terminal strings have multiple leftmost derivations from the start symbol.,S,if E then S,S,if E then S,if E then S else S,if E then S else S,Ambiguity,There is no general algorithm to tell whether a CFG is ambiguous or not. There is no standard procedure for eliminating ambiguity. Some languages are inherently ambiguous. In those cases, any grammar we come up with will be ambiguous.,Ambiguity,In general, we try to eliminate ambiguity by rewriting the grammar. Example: EE+E | EE | id becomes: EE+T | T TTF | F F id,Ambiguity,In general, we try to eliminate ambiguity by rewriting the grammar. Example: Sif E then S else S | if E then S | other becomes: S EwithElse | EnoElse EwithElse if E then EwithElse else EwithElse | other EnoElse if E then S | if E then EwithElse else EnoElse,Top-down parsing,Main idea: Start at the root, grow towards leaves Pick a production and try to match input May need to backtrack Example: Use the expression grammar to parse x-2*y,Grammar problems,Because we try to generate a leftmost derivation by scanning the input from left to right, grammars of the form A A x may cause endless recursion. Such grammars are called left-recursive and they must be transformed if we want to use a top-down parser.,Left recursion,A grammar is left recursive if for a non-terminal A, there is a derivation A+ A There are three types of left recursion: direct (A A x) indirect (A B C, B A ) hidden (A B A, B ),Left recursion,To eliminate direct left recursion replace A A1 | A2 | . | Am | 1 | 2 | . | n with A 1B | 2B | . | nB B 1B | 2B | . | mB | ,Left recursion,How about this: S E E E+T E T T E-T T id,There is direct recursion: EE+T There is indirect recursion: TE+T, ET,Algorithm for eliminating indirect recursion List the nonterminals in some order A1, A2, .,An for i=1 to n for j=1 to i-1 if there is a production AiAj, replace Aj with its rhs eliminate any direct left recursion on Ai,Eliminating indirect left recursion,S E E E+T E T T E-T T F F E*F F id,i=S,ordering: S, E, T, F,S E E E+T E T T E-T T F F E*F F id,i=E,S E E TE E+TE| T E-T T F F E*F F id,i=T, j=E,S E E TE E+TE| T TE-T T F F E*F F id,S E E TE E+TE| T FT T E-TT| F E*F F id,Eliminating indirect left recursion,i=F, j=E,S E E TE E+TE| T FT T E-TT| F TE*F F id,i=F, j=T,S E E TE E+TE| T FT T E-TT| F FTE*F F id,S E E TE E+TE| T FT T E-TT| F idF F TE*FF|,Grammar problems,Consider S if E then S else S | if E then S Which of the two productions should we use to expand non-terminal S when the next token is if? We can solve this problem by factoring out the common part in these rules. This way, we are postponing the decision about which rule to choose until we have more information (namely, whether there is an else or not). This is called left factoring,Left factoring,A 1 | 2 |.| n | becomes A B| B 1 | 2 |.| n,Grammar problems,A symbol XV is useless if there is no derivation from X to any string in the language (non-terminating) there is no derivation from S that reaches a sentential form containing X (non-reachable) Reduced grammar = a grammar that does not contain any useless symbols.,Useless symbols,In order to remove useless symbols, apply two algorithms: First, remove all non-terminating symbols Then, remove all non-reachable symbols. The order is important! For example, consider S + X where contains a non-terminating symbol. What will happen if we apply the algorithms in the wrong order? Concrete example: S AB | a, A a,Useless symbols,Example,Initial grammar: S AB | CA A a B CB | AB C cB | b D aD | d,Algorithm 1 (terminating symbols): A is in because of A a C is in because of C b D is in because of D d S is in because A, C are in and S AC,Useless symbols,Example continued,After algorithm 1: S CA A a C b D aD | d,Algorithm 2 (reachable symbols): S is in because it is the start symbol C and A are in because S is in and S CA,Final grammar: S CA A a C b,
展開閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
提示  裝配圖網(wǎng)所有資源均是用戶自行上傳分享,僅供網(wǎng)友學(xué)習(xí)交流,未經(jīng)上傳用戶書面授權(quán),請(qǐng)勿作他用。
關(guān)于本文
本文標(biāo)題:《英語(yǔ)語(yǔ)法描述》PPT課件.ppt
鏈接地址:http://m.italysoccerbets.com/article/117257952.html

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!