Parser

Syntax: the way in which words are put together to form phrases, clause, or sentence.


1.Context-free grammar

We learn before that we use regular expression and finite automata to construct lexical scanner. For example.

            digits=[0-9]+

            sum=(digits"+")*digits

This use the abbreviation of 'digits' and can express 'sum=12+43+33'. But now consider

            digits=[0-9]+

            sum=expr"+"expr

            expr="("sum")"|digits

This can express 'sum=1+(123+9)'. But it is impossible for a finite automata to recognize that expression since it is recursive. In short we cannot use regular expression and finite automata to express things like that. So we need one more powerful tools to support recursion: Context-free Grammar! 

The grammar consists of 4 part:

1.Terminal (symbol).which is tokens obtained from lexical analysis. 

2.Nonterminal (symbol). using the abbreviation and appears on the left-hand side of a production.

3.Production.like the form:symbol ——>symbol symbol ... symbol.

4.Start symbol.the symbol you start to derive a sentence.

Context-free grammar

2. Parse tree 

Through the derivation of grammar, we can obtain an parse tree. For example, we want to derive a := 7; b := c + (d := 5 + 6, d) in terms of grammar above. The derivation and parse tree shows below.


Derivation


`Parse tree
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 心境 二战时期,一位美国妇女背井离乡,离开繁花的中部城市,随丈夫迁往加利福尼亚州。丈夫是住防军人,营区靠近少漠,生...
    在家上学在线教育阅读 426评论 0 5
  • 文 /雁南飞 刘妍住进医院,安静是后来才知道的,她平时上班早出晚归的,那天晚上刘妍冲她发脾气,她知道嫂子是误会她了...
    雁南飞FM阅读 566评论 0 4
  • 有缘千里来相会,无缘对面不相识! 什么是爱? 爱的含义很多种,亲情,友情,爱情…… 先从亲情再有了友情,友情再有了...
    岁月的足迹阅读 255评论 0 1