文法和语言

  • 语言是一个记号系统,完整的定义包括语法和语义两方面。
  • 语法是一组说明语言的规则,文法是用来阐明这些语法规则的一个重要形式工具。
  • 语义包括静态语义和动态语义,阐明语义要比语法困难的多。

符号和符号串

字母表: 字母表是符号的非空有穷集合。

任何程序语言都有自己的字母表,例如:

  • 计算机语言:由符号“0”和“1”组成的字母表,即∑={0,1}
  • Pascal字母表为: ∑={AZ, az, 09, +, -, *, /, <, =, >,:,‘,’, ;,., , (, ), {, }, [, ]}

符号串:由字母表中的符号组成的任何有穷序列称之为该字母表上的符号串,也称作”字”。

符号串的几个常用术语 : 设s是符号串

  • 前缀 : 移走s的尾部的零个或多于零个符号
  • 后缀 : 删去s的头部的零个或多于零个符号
  • 子串 : 从s中删去一个前缀和一个后缀
  • 子序列 : 从s中删去零个或多于零个符号(不要求是连续的)
  • 逆转 : 将s中的符号按相反次序写出而得到的符号串。
  • 长度 : 是该符号串中的符号的数目。例 :|aab|=3,|ε|=0。

例 :符号串s=banana

  • 前缀:ε,b,ba,ban,bana,banan,banana
  • 后缀:banana,anana,nana,ana,na,a,ε
  • 子串:banana,anana,banan,anan,…,ε
  • 真前缀,真后缀,真子串:x≠s & x ≠ ε
  • 子序列:baa (这些符号不要求是连续的)
  • 逆转:ananab
  • 长度:| banana | = 6

符号串的运算 :

  • 连接:设x和y是符号串,它们的连接xy是把y的符号写在x的符号之后得到的符号串。
    例如:x=ba,y=nana,xy=banana.
  • 方幂:x0=ε; x1=x; x2=xx; …;xn=xn-1x;
    例: x=ba, 则: x1= ba; x2=baba; x3=bababa;…

符号串集合(语言)的运算 :

设L和M是两个符号串集合,则 :

  • 合并:L∪M={s|s∈L or s∈M}
  • 连接:LM={ st|s∈L and t∈M}
  • 方幂: L0={ε}, L1=L,L2=LL, …, Ln=Ln-1L
  • 语言L的闭包,记作L* ,L* =∪Li(i>=0) =L0∪L1∪L2∪L3 ∪…
  • 语言L的正闭包,记作L+(L+=L L* ), L+=∪Li(i>=1)=L1∪L2∪L3∪L4∪…

例如:L={A~Z,a~z} D={0~9}

  • L∪D={A~Z,a~z ,0~9}
  • LD是由所有用一个字母后跟一个数字组成的符号串所构成的集合。
  • L4是由所有的四个字母的符号串构的集合。
  • L(L∪D)* 是由所有的字母打头的字母和数字组成的符号串所构成的集合.
  • D+是由所有的长度大于等于1的数字串所构成的集合.

文法和语言的形式定义

文法G定义为四元组 (VN,VT,P,S)
其中, VN :非终结符号集; VT :终结符号集; P:规则的集合; S:开始符(识别符)。
注: VN, VT和 P 是非空有穷集。S是一个非终结符,它至少要在一条产生式中作为左部出现。VN和VT不含公共的元素,即VN∩VT,用V表示VN∪VT ,称为文法G的字母表。

文法G(VN,VT,P,S)中规则集合P的说明 

  • 12
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java代码的文法可以用BNF(巴克斯-诺尔范式)表示,如下所示: ``` <compilationUnit> ::= [<packageDeclaration>] [<importDeclaration>] {<typeDeclaration>} <packageDeclaration> ::= "package" <qualifiedName> ";" <importDeclaration> ::= "import" ["static"] <qualifiedName> ["." "*"] ";" <typeDeclaration> ::= <classDeclaration> | <interfaceDeclaration> <classDeclaration> ::= {<modifier>} "class" <identifier> [<typeParameters>] [<superclass>] [<superinterfaces>] <classBody> <interfaceDeclaration> ::= {<modifier>} "interface" <identifier> [<typeParameters>] [<extendsInterfaces>] <interfaceBody> <modifier> ::= "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "transient" | "volatile" | "strictfp" <typeParameters> ::= "<" <typeParameterList> ">" <typeParameterList> ::= <typeParameter> {"," <typeParameter>} <typeParameter> ::= <identifier> [<extendsBound>] <extendsBound> ::= "extends" <type> [<additionalBound> {"," <additionalBound>}] <additionalBound> ::= "extends" <type> <superclass> ::= "extends" <classType> <superinterfaces> ::= "implements" <interfaceTypeList> <classBody> ::= "{" {<classBodyDeclaration>} "}" <classBodyDeclaration> ::= <classMemberDeclaration> | <staticInitializer> | <constructorDeclaration> <classMemberDeclaration> ::= <fieldDeclaration> | <methodDeclaration> | <classDeclaration> | <interfaceDeclaration> | <enumDeclaration> <staticInitializer> ::= "static" <block> <constructorDeclaration> ::= {<modifier>} <constructorDeclarator> <throws> <constructorBody> <constructorDeclarator> ::= <identifier> "(" [<formalParameterList>] ")" <formalParameterList> ::= <formalParameter> {"," <formalParameter>} <formalParameter> ::= {<annotation>} [<final>] <type> <variableDeclaratorId> <variableDeclaratorId> ::= <identifier> [<dims>] <dims> ::= "[" "]" <methodDeclaration> ::= {<modifier>} <type> <methodDeclarator> <throws> [<methodBody> | <nativeMethodBody>] <methodDeclarator> ::= <identifier> "(" [<formalParameterList>] ")" <methodBody> ::= <block> <nativeMethodBody> ::= "native" ";" <fieldDeclaration> ::= {<modifier>} <type> <variableDeclarators> ";" <variableDeclarators> ::= <variableDeclarator> {"," <variableDeclarator>} <variableDeclarator> ::= <variableDeclaratorId> ["=" <variableInitializer>] <variableInitializer> ::= <expression> | <arrayInitializer> <arrayInitializer> ::= "{" [<variableInitializer> {"," <variableInitializer>}] [","] "}" <block> ::= "{" {<blockStatement>} "}" <blockStatement> ::= <localVariableDeclarationStatement> | <statement> <localVariableDeclarationStatement> ::= <type> <variableDeclarators> ";" <statement> ::= <expressionStatement> | <declarationStatement> | <ifStatement> | <switchStatement> | <whileStatement> | <doStatement> | <forStatement> | <breakStatement> | <continueStatement> | <returnStatement> | <throwStatement> | <tryStatement> <expressionStatement> ::= [<expression>] ";" <declarationStatement> ::= <localVariableDeclarationStatement> <ifStatement> ::= "if" "(" <expression> ")" <statement> ["else" <statement>] <switchStatement> ::= "switch" "(" <expression> ")" "{" {<switchBlockStatementGroup>} "}" <switchBlockStatementGroup> ::= <switchLabels> <blockStatements> <switchLabels> ::= <switchLabel> {<switchLabel>} <switchLabel> ::= "case" <constantExpression> ":" | "default" ":" <blockStatements> ::= {<blockStatement>} <whileStatement> ::= "while" "(" <expression> ")" <statement> <doStatement> ::= "do" <statement> "while" "(" <expression> ")" ";" <forStatement> ::= "for" "(" [<forControl>] ")" <statement> <forControl> ::= <enhancedForControl> | <forInit> ";" [<expression>] ";" [<forUpdate>] <enhancedForControl> ::= <type> <variableDeclaratorId> ":" <expression> <forInit> ::= <localVariableDeclarationStatement> | [<expressionList>] <forUpdate> ::= <expressionList> <expressionList> ::= <expression> {"," <expression>} <breakStatement> ::= "break" [<identifier>] ";" <continueStatement> ::= "continue" [<identifier>] ";" <returnStatement> ::= "return" [<expression>] ";" <throwStatement> ::= "throw" <expression> ";" <tryStatement> ::= "try" <block> <catches> [<finally>] | "try" <resourceSpecification> <block> [<catches>] [<finally>] <resourceSpecification> ::= "(" {<resource>} [","] ")" <resource> ::= <variableModifiers> <classType> <variableDeclaratorId> "=" <expression> <catches> ::= {<catchClause>} <catchClause> ::= "catch" "(" <catchFormalParameter> ")" <block> <catchFormalParameter> ::= [<final>] <catchType> <variableDeclaratorId> <catchType> ::= <qualifiedName> { "|" <qualifiedName>} <finally> ::= "finally" <block> <annotation> ::= "@" <qualifiedName> [<elementValuePairs>] | "@" <qualifiedName> "(" [<elementValuePairs>] ")" <elementValuePairs> ::= <elementValuePair> {"," <elementValuePair>} <elementValuePair> ::= <identifier> "=" <elementValue> <elementValue> ::= <expression> | <annotation> | <elementValueArrayInitializer> <elementValueArrayInitializer> ::= "{" {<elementValue>} [","] "}" ``` 这个文法描述了Java代码的语法结构,可以用于编写Java编译器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值