Pascal语言的文法实例
时间: 2023-11-30 10:02:04 浏览: 49
Pascal语言的文法实例如下:
```
program MyProgram;
var
age: integer;
begin
writeln('Please enter your age:');
readln(age);
if (age < 18) then
writeln('You are not yet an adult.')
else if (age >= 18) and (age < 65) then
writeln('You are an adult.')
else
writeln('You are a senior citizen.');
end.
```
其中,文法规则包括:
```
program -> program id ;
declaration-part
statement-part
id -> identifier
declaration-part -> var declaration-list | ε
declaration-list -> declaration ; | declaration-list declaration ;
declaration -> identifier-list : type
identifier-list -> identifier | identifier-list , identifier
type -> integer | real | boolean
statement-part -> compound-statement
compound-statement -> begin statement-list end
statement-list -> statement | statement-list ; statement
statement -> compound-statement | assignment-statement | if-statement | writeln-statement | readln-statement
assignment-statement -> variable := expression
variable -> identifier
expression -> simple-expression | simple-expression relop simple-expression
simple-expression -> term | sign term | simple-expression addop term
term -> factor | term mulop factor
factor -> variable | constant | ( expression ) | not factor
constant -> integer-constant | real-constant | boolean-constant
if-statement -> if expression then statement else-part
else-part -> else statement | ε
writeln-statement -> writeln ( expression-list ) ;
expression-list -> expression | expression-list , expression
readln-statement -> readln ( variable-list ) ;
variable-list -> variable | variable-list , variable
relop -> = | <> | < | <= | > | >=
addop -> + | - | or
mulop -> * | / | div | mod | and
sign -> + | -
```
以上是Pascal语言的基本文法规则,包括了程序框架、变量声明、语句执行等方面。在实际编写Pascal程序时,还需要遵循各种语法规则和语义规则。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)