ImageVerifierCode 换一换
格式:PPTX , 页数:62 ,大小:1.26MB ,
资源ID:12544986      下载积分:14 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/12544986.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(Programming-Language-Syntax.pptx)为本站上传会员【w****g】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

Programming-Language-Syntax.pptx

1、Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Lecture Note#2,:Ch2.Programming Language Syntax,Programming Language Pragmatics,Term 1,2023,Minyoung Kim,1,Programming LanguageSyntax Notations,2,Simple Syntax Notations,|,means“or”,

2、e.g.digit,-,0|1|2|3|4|5|6|7|8|9,*(pronounced as“Kleene star”),indicates zero or more repetitions of the symbol to its left,e.g.,digit,-,0|1|2|3|4|5|6|7|8|9,non_zero_digit,-,1|2|3|4|5|6|7|8|9,natural_number,-,non_zero_digit digit*,Semantics,3,Digits in the previous are only symbols,ink blobs on paper

3、 or pixels on a screen,no meanings in and of themselves,Semantics can add meanings to symbols,The digits mean natural numbers from zero to nine,as defined by mathematicians,May represent colors,the days of the week in a decimal calendar,May have alternative semantics with the same syntax,The reasons

4、 of distinguishing between syntax and semantics,Different programming languages often provide features with similar semantics but very different syntax,Efficient and elegant algorithms to discover the syntactic structure is available,2.1 Specifying Syntax,4,Formal specification of syntax requires a

5、set of rules,Tokens can be specified using just three kinds of rules(Regular expressions),A Character,Empty string,denoted by,Concatenation-two regular expressions concatenated,alternation(choice among a finite set of alternatives),separated by|,Kleene closure(concatenation of zero or more strings),

6、Specifying Syntax,5,Syntax can be specified with 4 kinds of rules(Context-free Grammars),concatenation,alternation(choice among a finite set of alternatives),Kleene closure(repetition an arbitrary number of times),recursion(creation of a construct from simpler instances of the same construct),contex

7、t-free language(CFL)is the string generated by the above rules,Tokens and Regular Expressions,6,Tokens are basic building blocks of programs,including keywords,identifiers,numbers,punctuation marks,and other symbols,Pascal has 64 kinds of tokens,21 symbols(,+,-,;,:=,.,etc.),35 keywords(begin,end,div

8、record,while,etc),integer literals(e.g.,137),real(floating-point)literals(6.022e23),character/string literals(e.g.,snerk),identifiers(MyVariable,YourType,maxint,readln,etc.,39 of which are predefined),two different kinds of comments(*),Tokens and Regular Expressions,7,C has 100 kinds of tokens,37 k

9、eywords(double,if,return,struct,etc.),identifiers(my_variable,your_type,sizeof,printf,etc.),integer(0765,0 x1f5,501),floating-point(6.022e23),character(x,0170),string literals(“snerk”,“say”hi”n”),54 punctuators(,+,-,*=,:,|,etc.),35 keywords(begin,end,div,record,while,etc),two different kinds of comm

10、ents/*/,/,Preprocessor macros that build tokens from smaller pieces,Specifying Tokens Use Regular Expressions,8,A regular expression is one of the following:,A character,The empty string,denoted by,Two regular expressions concatenated,Two regular expressions separated by|(i.e.,or),A regular expressi

11、on followed by the Kleene star(concatenation of zero or more strings),parenthesis are used to avoid ambiguity,Tokens and Regular Expressions,9,Numeric literals of Pascal can be generated with the following regular expression,digit,0|1|2|3|4|5|6|7|8|9,unsigned_integer,digit,digit,*,unsigned _number,u

12、nsigned_integer,(.,unsigned_integer,)|)(e|E),(+|-|),unsigned_integer,)|),E.g.,1 1.3,1e31.3e-31.3938e+3,1.1.3.4931.e3,Simple Hand-held Calculator Language,10,Syntax of numeric constants accepted by a simple hand-held calculators,number,integer|real,integer,digit digit*,real integer exponent|decimal(e

13、xponent|,),decimal digit*(.digit|digit.)digit*,exponent (e|E)(,+,|,-,|,)integer,digit 0|1|2|3|4|5|6|7|8|9,Tokens,11,Case-sensitiveness,sensitive:Modula-2/3,C/C+,Java,Keywords in C/C+are lowercase,whereas they are uppercase in Modula-2/3,not sensitive:Ada,Common Lisp,Fortran 90,Pascal,Modula-2 and Mo

14、dula-3 require keywords and predefined identifiers to be written in uppercase,whereas C and its variants uses lowercase,Limitations in Identifiers,Only allow letters and digits:Modula-3 and Standard Pascal,Allow letters,digits,and underscores:C/C+,C#,Pascal(most implementations),A variety of additio

15、nal characters:Lisp(even includes,-,),Some language impose limits on the maximum length of identifiers,Old Turbo C compiler,Tokens,12,Most modern languages considers a program as simply a sequence of tokens:,“White space”(blanks,tabs,carriage returns,and line and page feed characters)between tokens

16、is usually ignored,Fortran prior to Fortran 90 use a fixed format,with 72 characters(the width of an IBM punch card)per line,different columns within the line reserved for different purposes,e.g.1-5 for labels,6 for continuation of lines,7-72 source code,Line breaks serve to separate statements in s

17、everal other languages,Haskell,Occam,and SR,Some languages treats indentation specially,Haskell,Occam,Python,Tokens,13,Many modern languages,including C99,C+,Ada 95,Java,C#,and Fortran 2023 have explicit support for multibyte character sets,generally based on Unicode and ISO/IEC 10646,Most modern la

18、nguages allow non-Latin characters to appear within comments and character strings,Some languages allow non-Latin characters in identifiers,Context-Free Grammars,14,Regular expressions cannot be used to specify,nested,constructs,E.g.the structure of an arithmetic expression,(Grammar Example 2.4),exp

19、r,id|number|,-,expr,|(,expr,),|,expr operator expr,op,+,|,-,|*|/,Each rule in a CFG is a,production,Symbols on the left-hand sides of the productions are,variables,Symbols that are to make up the strings derived from the grammar are terminals,cannot appear on the left-hand side of any production,var

20、iable,production,terminal,a 7 -a -4 (a),(12)a+4 -(3*6),Context-Free Grammars,15,The notation for context-free grammars(CFG)is sometimes called Backus-Naur Form(BNF),A CFG consists of,A set of terminals T,A set of non-terminals N,A start symbol S(a non-terminal),A set of productions,BNF doesnt allow

21、using the vertical bar,Kleene star,and meta-level parentheses of regular expressions,EBNF(Extended BNF),Allows the extra operators including Kleene plus,Kleene plus(,+,):indicates one or more instances of the symbol or groups of symbols in front of it,digit*:0,digit+:1,Context-Free Grammars,16,BNF(E

22、xample),op,+|,-,|*|/(not allowed using|sign),can be rewritten as,op,+,op,-,op,*,op,/,id_list,id(,id)*,(not allowed using Kleene star),can be rewritten as,id_list,id,id_list,id_list,id,identifier ,identifier ,identifier,Derivation and Parse Trees,17,A context-free grammar shows how to generate syntac

23、tically valid strings of terminals:,Begins with the start symbol,Choose a production with the start symbol on the left-hand side,Replace the start symbol with the right-handed side of that production(choose a nonterminal A in the resulting string,choose a production P with A on its left-hand side,an

24、d replace A with the right-hand side of P.,Repeat this process until no nonterminal remain,Derivation and Parse Trees,18,to generate“slope*x+intercept”,(Right most derivation:a*x+b),Grammar Example 2.6,expr,=,expr op,expr,=,expr,op,id,=,expr,+id,=,expr op,expr,+id,=,expr,op,id +id,=,expr,*id +id,=,i

25、d *id +id,(slope)(x)(intercept),Derivations and Parse Trees,19,A,parse tree,can be used to represent a derivation grammar in grammar Example 2.4,Right-most derivation:(slope*x)+intercept,Fig.2.1,Left-most derivation:slope*(x+intercept),Fig.2.2,Alternative(undesirable)parse tree in this case,Derivati

26、ons and Parse Trees,20,More than one tree exists implies that the grammar is,ambiguous,Structure is particularly important in arithmetic expressions,Need to use,productions to capture the,associativity,and,precedence,Associativity:,the operators in most language group left-to-right,Precedence:*and/i

27、n most language group are computed more,Derivations and Parse Trees,21,A better version of expression grammar,without ambiguity,Grammar Example 2.8,expr,term,|,expr add_op term,term,factor,|,term multi_op factor,factor,id|number|-,factor,|(,expr,),add_op,+|-,multi_op,*|/,Draw a Parse Tree for 10*4 3

28、with Left Associativity,22,Left most derivation in grammar Example 2.8,expression,=,term,=,term,multi_op factor,=,factor,multi_op factor,=,number,multi_op,factor,=,number*,factor,This is not processed,expression,term,|,expression,add_op term,term,factor,|,term multi_op factor,factor,identifier|numb

29、er|-,factor,|(,expression,),add_op,+/-,multi_op,*|/,Draw a Parse Tree for 10*4 3,with Precedence,23,Right most derivation in grammar Example 2.8,expression,term,|,expression,add_op term,term,factor,|,term multi_op factor,factor,identifier|number|-,factor,|(,expression,),add_op,+/-,multi_op,*|/,expre

30、ssion,=,expression add_op,term,=expression add_op,factor,=,expression,add_op,number,=,expression,number,=,term multi_op,factor,number,=term,multi_op,number number,=,term,*number number,=,factor,*number number,=number*number number,(10)(4)(3),Thus,grammar 2.8,is clear,Draw a Parse Tree for 10*4 3,wit

31、h Precedence,24,Parse tree for expression grammar for 10*4-3,Draw a Parse Tree for a+4*5,with Precedence,25,Left most derivation in grammar Example 2.8,expression,term,|,expression,add_op term,term,factor,|,term multi_op factor,factor,identifier|number|-,factor,|(,expression,),add_op,+/-,multi_op,*|

32、/,expression,=,expression add_op,term,=expression add_op term multi_op,factor,=,expression add_op term,multi_op,number,=,expression add_op,term,*number,=,expression add_op,factor,*number,=expression,add_op,number*number,=,expression,+number*number,=,factor,+number*number,=,identifier,+number*number,

33、a)(4)(5),expression,=,term,=,term multi_op,factor,=term,multi_op,number,=,term,*number,!,Draw a Parse Tree for a+4*5,with Precedence,26,Parse tree for expression grammar for a+4*5,Draw a Parse Tree for 10 4 3,with Precedence,27,Derivation in grammar Example 2.8,expression,term,|,expression,add_op t

34、erm,term,factor,|,term multi_op factor,factor,identifier|number|-,factor,|(,expression,),add_op,+/-,multi_op,*|/,expression,=,expression add_op,term,=expression add_op,factor,=,expression,add_op,number,=,expression,-number,=,expression add_op,term,-number,=expression,add_op,number-number,=,expressio

35、n,-number-number,=,term,-number-number,=number-number-number,(10)(4)(3),terminal,!,Draw a Parse Tree for 10 4 3,with Precedence,28,Parse tree for expression grammar for 10 4 3,2.2 Recognizing Syntax:Scanners and Parsers,29,Recall scanner is responsible for,tokenizing source,removing comments,nested

36、comment needs special treatments because scanners normally do not deal with recursive constructs,some language disallow them,others may put special code in the scanner to deal with it,(often)dealing with pragmas(i.e.,significant comments),saving text of identifiers,numbers,strings,saving source loca

37、tions(file,line,column)for error messages,Recognizing Syntax:Scanners and Parsers,30,Parser in charge of the compilation process,calls the scanner to obtain the tokens of the input program,assembles the tokens together into a parse tree,passes the tree to the later phases of the compiler,which perfo

38、rm semantic analysis,code generation and improvement,Scanner and parser for a programming language are responsible for discovering the syntactic structure of a program,Scanning,31,Will create a simple“calculator language”with input,output,variables,and assignment extending the grammar shown in Examp

39、le 2.4 and 2.8,Tokens for the simple calculator language,assign,:=,plus,+,minus,times,*,div,/,l-paren,(,r-paren,),id,letter,(,letter,|,digit,)*except for,read,and,write,number digit digit*,(.,digit,|,digit,.),digit*,comment,/*,(non-*,|,*non-,/,)*,/,|,/,(non-newline)*newline,Scanning,How do we build

40、a scanner that recognizes the tokens of our calculator language?,build ad hoc scanner,Right shows the outline of an ad hoc scanner for the simple calculator language,32,Fig.2.5,Scanning,33,Deterministic finite automation for calculator language,Scanning,34,As a rule,the scanner returns the longest p

41、ossible token in each invocation of the scanner,e.g.foobar is always foobar and never f or foo or foob,In language like C,3.14159 is a real number and never 3,.,and 14159,White space(blanks,tabs,carriage returns,comments)is generally ignored except to the extent that it separates tokens,e.g.foo bar

42、is different from foobar,Scanning,35,Scanners tend to be built three ways,ad-hoc,manually constructed,generally yields the fastest,most compact code by doing lots of special-purpose things,semi-mechanical pure DFA(usually realized as nested case statements),either manually or using a,scanner generat

43、or,table-driven DFA,usually built with a,scanner generator,such as Lex,Scanning,36,This is a deterministic finite automaton(DFA),Lex,scangen,etc.build these things automatically from a set of regular expressions,Specifically,they construct a machine that accepts the language,id|int const|real const|

44、comment|symbol|.,We run the machine over and over to get one token after another,Nearly universal rule:,always take the longest possible token from the input thus foobar is foobar and never f or foo or foob,more to the point,3.14159 is a real const and never 3,.,and 14159,Regular expressions generat

45、e a regular language;DFAs recognize it,Scanning,37,We can implement a scanner that explicitly captures the“circles-and-arrows”structure of a DFA in either of two main ways,Embeds the automaton in the control flow of the program using,goto,s or nested,case(switch),Writing a pure DFA as a set of neste

46、d case(switch in C/C+)statements is a surprisingly useful programming technique,though its often easier to use perl,awk,sed,Scanning,38,Note that the rule about longest-possible tokens means you return only when the next character cannot be used to continue the current token,the next character will

47、generally need to be saved for the next token,In some cases,you may need to peek at more than one character of look-ahead in order to know whether to proceed,In Pascal,for example,when you have a 3 and you a see a dot,do you proceed(in hopes of getting 3.14)?or,do you stop(in fear of getting 3.5)?,S

48、canning Special Handling,39,Keywords in most languages(including Pascal),Look like identifiers,Specially reserved for a special purpose,Writing a finite automaton that distinguishes keywords and identifiers,but requires a lot of states,Thus,treated as“exceptions”to the rule for identifiers(before re

49、turning an identifier to the parser,the scanner looks it up in a hash table or trie to make sure it isnt really a keyword,Dot-dot problem in Pascal,Needs to distinguish 3.14,3.14,or 3.foo,If the scanner has seen a 3 and has a dot(.)coming up in the input,it needs to peek at the character beyond the

50、dot to distinguish 3.14 and 3.14(valid syntax)or 3.foo(invalid syntax),Scanning,40,Table-driven DFA is what lex and scangen produce,Represents the automaton as a data structure:a two-dimensional table,A driver program uses the current state and input character to index into the table,Each entry in t

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服