pascal程序设计语言文法实例
时间: 2023-08-21 21:05:47 浏览: 92
pascal 语言文法
以下是 Pascal 程序设计语言的文法实例:
```
<program> ::= program <identifier> ;
<block> .
<block> ::= <declaration-part>
<statement-part>
<declaration-part> ::= <label-declaration-part>
<constant-definition-part>
<type-definition-part>
<variable-declaration-part>
<procedure-and-function-declaration-part>
<label-declaration-part> ::= label <label> { , <label> } ;
<label> ::= <unsigned-integer>
<constant-definition-part> ::= const <constant-definition> { ; <constant-definition> } ;
<constant-definition> ::= <identifier> = <constant>
<constant> ::= <unsigned-number>
| <sign> <unsigned-number>
| <string>
| <identifier>
<type-definition-part> ::= type <type-definition> { ; <type-definition> } ;
<type-definition> ::= <identifier> = <type>
<type> ::= <simple-type>
| <structured-type>
| <pointer-type>
<simple-type> ::= <scalar-type>
| <subrange-type>
| <enumerated-type>
| <set-type>
<scalar-type> ::= <identifier>
<subrange-type> ::= <constant> .. <constant>
<enumerated-type> ::= ( <identifier> { , <identifier> } )
<set-type> ::= set of <base-type>
<base-type> ::= <scalar-type>
<structured-type> ::= <array-type>
| <record-type>
<array-type> ::= array [ <index-range> { , <index-range> } ] of <component-type>
<index-range> ::= <simple-type>
<component-type> ::= <type>
<record-type> ::= record <field-list> end
<field-list> ::= <fixed-part>
| <fixed-part> ; <variant-part>
| <variant-part>
<fixed-part> ::= <record-section> { ; <record-section> } ;
<variant-part> ::= case <tag-field> of <variant> { ; <variant> } ; [ else <field-list> ] end ;
<tag-field> ::= <identifier> : <type>
<variant> ::= <variant-selector> : ( <field-list> )
<variant-selector> ::= <constant> .. <constant>
| <identifier>
<variable-declaration-part> ::= var <variable-declaration> { ; <variable-declaration> } ;
<variable-declaration> ::= <identifier> { , <identifier> } : <type>
<procedure-and-function-declaration-part> ::= <procedure-declaration>
| <function-declaration>
| <procedure-declaration> ; <function-declaration>
<procedure-declaration> ::= procedure <identifier> ; [ <formal-parameter-list> ] ; <block> ;
<function-declaration> ::= function <identifier> ; [ <formal-parameter-list> ] : <type> ; <block> ;
<formal-parameter-list> ::= ( [ <parameter-group> { ; <parameter-group> } ] )
<parameter-group> ::= <identifier-list> : <parameter-type>
<identifier-list> ::= <identifier> { , <identifier> }
<parameter-type> ::= <simple-type>
| <structured-type>
| <identifier>
<statement-part> ::= <compound-statement>
<compound-statement> ::= begin <statement> { ; <statement> } end
<statement> ::= <simple-statement>
| <structured-statement>
<simple-statement> ::= <assignment-statement>
| <procedure-call-statement>
| <goto-statement>
| <empty-statement>
<assignment-statement> ::= <variable-access> := <expression>
<procedure-call-statement> ::= <identifier> [ <actual-parameter-list> ]
<actual-parameter-list> ::= <expression> { , <expression> }
<goto-statement> ::= goto <label>
<empty-statement> ::=
<structured-statement> ::= <compound-statement>
| <conditional-statement>
| <repetitive-statement>
| <with-statement>
<conditional-statement> ::= if <expression> then <statement> [ else <statement> ]
<repetitive-statement> ::= <while-statement>
| <repeat-statement>
| <for-statement>
<while-statement> ::= while <expression> do <statement>
<repeat-statement> ::= repeat <statement> { ; <statement> } until <expression>
<for-statement> ::= for <control-variable> := <for-list> do <statement>
<control-variable> ::= <identifier>
<for-list> ::= <initial-value> [ to | downto ] <final-value>
<initial-value> ::= <expression>
<final-value> ::= <expression>
<with-statement> ::= with <record-variable-list> do <statement>
<record-variable-list> ::= <variable-access> { , <variable-access> }
<expression> ::= <simple-expression>
| <simple-expression> <relational-operator> <simple-expression>
<relational-operator> ::= = | <> | < | <= | >= | >
<simple-expression> ::= <term>
| <sign> <term>
| <simple-expression> <adding-operator> <term>
<sign> ::= + | -
<adding-operator> ::= + | - | or
<term> ::= <factor>
| <term> <multiplying-operator> <factor>
<multiplying-operator> ::= * | / | div | mod | and
<factor> ::= <variable-access>
| <unsigned-constant>
| ( <expression> )
| <function-designator>
<variable-access> ::= <variable> { <indexed-selector> | <field-selector> }
<variable> ::= <identifier>
<indexed-selector> ::= [ <expression> { , <expression> } ]
<field-selector> ::= . <identifier>
<unsigned-constant> ::= <unsigned-number>
| <string>
| <constant-identifier>
<function-designator> ::= <identifier> [ <actual-parameter-list> ]
<string> ::= '<character> { <character> }'
<constant-identifier> ::= <identifier>
```
注意:这只是 Pascal 的一个简单实现,实际上 Pascal 的语法非常复杂,因此这里只提供了一部分重要的规则。
阅读全文