• Tidak ada hasil yang ditemukan

Appendix C:

N/A
N/A
Protected

Academic year: 2025

Membagikan "Appendix C:"

Copied!
2
0
0

Teks penuh

(1)

Appendix C:

Context-free grammars and I/O facilities for Parva

The following LL(1) grammar describes the Parva language for which a compiler is developed in chapter 14.

COMPILER Parva

/* LL(1) grammar for Parva level 2 - syntax only */

CHARACTERS

cr = CHR(13) .

lf = CHR(10) .

backslash = CHR(92) .

control = CHR(0) .. CHR(31) .

letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

+ "abcdefghijklmnopqrstuvwxyz" . digit = "0123456789" .

stringCh = ANY - '"' - control - backslash . charCh = ANY - "'" - control - backslash . printable = ANY - control .

TOKENS

identifier = letter { letter | digit | "_" } . number = digit { digit } .

stringLit = '"' { stringCh | backslash printable } '"' . charLit = "'" ( charCh | backslash printable ) "'" . COMMENTS FROM "//" TO lf

COMMENTS FROM "/*" TO "*/"

IGNORE CHR(9) .. CHR(13) PRODUCTIONS

Parva = { FuncOrGlobalVarDeclarations

| ConstDeclarations } .

FuncOrGlobalVarDeclarations = Type Ident ( Function | GlobalVars ) .

Type = "void" | BasicType [ "[]" ] .

BasicType = "int" | "bool" .

Function = "(" FormalParameters ")" Block . GlobalVars = { "," Ident } ";" .

FormalParameters = [ OneParam { "," OneParam } ] .

OneParam = Type Ident .

Block = "{" { Statement } "}" .

Statement = Block | AssignmentOrCall | ";"

| ConstDeclarations | VarDeclarations

| IfStatement | WhileStatement

| HaltStatement | ReturnStatement

| ReadStatement | WriteStatement . ConstDeclarations = "const" OneConst { "," OneConst } ";" .

OneConst = Ident "=" Constant .

Constant = IntConst | CharConst

| "true" | "false" | "null" . VarDeclarations = Type OneVar { "," OneVar } ";" .

OneVar = Ident [ "=" Expression ] .

AssignmentOrCall = ( IF (IsCall())

Ident "(" Arguments ")"

| Designator "=" Expression ) ";" .

Designator = Ident [ "[" Expression "]" ] .

Arguments = [ OneArg { "," OneArg } ] .

OneArg = Expression .

IfStatement = "if" "(" Condition ")" Statement . WhileStatement = "while" "(" Condition ")" Statement . HaltStatement = "halt" ";" .

ReturnStatement = "return" [ Expression ] ";" .

ReadStatement = "read"

"(" ReadElement { "," ReadElement }

")" ";" .

ReadElement = StringConst | Designator .

Appendix C (Revised Extracts, June 2021) 210

(2)

WriteStatement = "write"

"(" WriteElement { "," WriteElement }

")" ";" .

WriteElement = StringConst | Expression .

Condition = Expression .

Expression = AddExp [ RelOp AddExp ] .

AddExp = [ "+" | "-" ] Term { AddOp Term } .

Term = Factor { MulOp Factor } .

Factor = ( IF (IsCall())

Ident "(" Arguments ")"

| Designator )

| Constant

| "new" BasicType "[" Expression "]"

| "!" Factor | "(" Expression ")" . RelOp = "==" | "!=" | ">" | ">=" | "<" | "<=" . AddOp = "+" | "-" | "||" .

MulOp = "*" | "/" | "%" | "&&" .

Ident = identifier .

StringConst = stringLit .

CharConst = charLit .

IntConst = number .

END Parva.

class IO {

public static int ReadInt()

// Reads a word as a textual representation of an integer // and returns the corresponding value.

// Errors may be reported or quietly ignored (when 0 is // returned).

public static bool ReadBool()

// Reads a word and returns a Boolean value, based on the // first letter. Typically the word would be T(rue) or Y(es) // or F(alse) or N(o).

public static char ReadChar()

// Reads and returns a single character.

public static string ReadString()

// Reads and returns a string. Incorporates leading white space, // and terminates on a control character, which is not

// incorporated or consumed.

public static string ReadLine()

// Reads and returns a string. Incorporates leading white space, // and returns when EOL or EOF is reached. The EOL character // is not incorporated, but is consumed.

public static string ReadWord()

// Reads and returns a word - a string delimited at either // end by a control character or space (typically the

// latter). Leading spaces are discarded, and the terminating // character is not consumed.

public static void Write(int x, int w) public static void Write(bool x, int w) public static void Write(char x, int w) public static void Write(string x, int w)

// Writes representation of the value of x to standard output.

// If w = 0, x is preceded by exactly one space

// If w > 0, x is right justified in a field of at least // w characters

// If w < 0, x is left justified in a field of at least // -w characters

public static void Write(int x) { Write(x, 0); } public static void Write(bool x) { Write(x, 0); } public static void Write(char x) { Write(x, 1); } public static void Write(string x) { Write(x, 1); } }

Appendix C (Revised Extracts, June 2021) 211

Referensi

Dokumen terkait