+/EE379K

programming paradigms

git clone https://git.julia.blue/+/EE379K

Joseph RyanReplace unicode minus signs with ascii4d0f68b

master
4.8 KiB186 linesraw
1***Intro***
2
3Write a lexer and parser (using JavaCC) and simple semantics analyzer
4for simple Java-like language described in this file.
5
6 - Each program starts with a keyword "program" and has static fields,
7   static methods, inerfaces, and inner classes.
8 - The main method in the language is always called main().
9 - There are:
10   -- three types of constants: int, char, and bool.
11   -- several primitive types: int, bool, char (ASCII only), enums.
12   -- several kinds of variables: global (static), local, fields.
13   -- reference types: arrays like in Java, interfaces and inner
14      classes with fields and methods.
15   -- static methods
16 - There is also class inheritance and polymorphism.
17 - Overloading is also available.
18 - Three predefined procedure include: ord, chr, and len.
19 - The "print" method prints values for all primitive types.
20 - Control structures include "if" and "for".
21
22***Syntax***
23
24Program = "program" ident {ConstDecl | VarDecl | ClassDecl| EnumDecl | InterfaceDecl} "{" {MethodDecl} "}".
25
26ConstDecl = "const" Type ident "=" (numConst | charConst| boolConst) {, ident "=" (numConst | charConst | boolConst)} ";".
27
28EnumDecl = "enum" ident "{" ident ["=" numConst] {"," ident ["=" numConst]} "}".
29
30VarDecl = Type ident ["[" "]"] {"," ident ["[" "]"]} ";".
31
32ClassDecl = "class" ident ["extends" Type] ["implements" Type {"," Type}]"{"  {VarDecl}["{"{MethodDecl} "}"]"}".
33
34InterfaceDecl = "interface" ident "{"{InterfaceMethodDecl} "}"
35
36InterfaceMethodDecl = (Type | "void") ident "(" [FormPars]")" ";"
37
38MethodDecl = (Type | "void") ident "(" [FormPars]")" {VarDecl} "{" {Statement} "}".
39
40FormPars = Type ident ["[" "]"] {"," Type ident ["[" "]"]}.
41
42Type = ident.
43
44Statement =  DesignatorStatement ";"
45  | "if" "(" Condition ")" Statement ["else" Statement]
46  | "for" "(" [DesignatorStatement ] ";" [Condition] ";" [DesignatorStatement] ")" Statement
47  | "break" ";"
48  | "continue" ";"
49  | "return" [Expr] ";"
50  | "read" "(" Designator ")" ";"
51  | "print" "(" Expr ["," numConst] ")" ";"
52  | "{" {Statement} "}".
53
54DesignatorStatement = Designator (Assignop Expr | "(" [ActPars] ")" | "++" | "--").
55
56ActPars = Expr {"," Expr}.
57
58Condition = CondTerm {"||" CondTerm}.
59
60CondTerm = CondFact {"&&" CondFact}.
61
62CondFact = Expr [Relop Expr].
63
64Expr = ["-"] Term {Addop Term}.
65
66Term = Factor {Mulop Factor}.
67
68Factor = Designator ["(" [ActPars] ")"]
69  | numConst
70  | charConst
71  | boolConst
72  | "new" Type ["[" Expr "]"]
73  | "(" Expr ")".
74
75Designator = ident {"." ident | "[" Expr "]"}
76
77Assignop = "=".
78
79Relop = "==" | "!=" | ">" | ">=" | "<" | "<="
80
81Addop = "+" | "-".
82
83Mulop = "*" | "/" | "%"
84
85
86***Other lexical structures***
87
88keywords :program, break, class, interface, enum, else, const, if,
89new, print, read, return, void, for, extends, continue
90
91token types:
92    ident = letter {letter | digit | "_"}.
93    numConst = digit {digit}.
94    charConst = "'" printableChar "'".
95    boolConst = ("true" | "false")
96
97operators: +, -, *, /, %, ==, !=, >, >=, <, <=, &&, ||, =, ++, --, ;,
98comma, ., (, ), [, ], {, }
99
100comments: // until the end of the line
101
102
103***Semantics***
104
105 - Each name must be declared before its fist use.
106 - There cannot be two variables with the same name in one scope.
107 - There must be a method "main"; the return type must be void and the
108   method should have no arguments.
109
110These checks should be done on the AST.
111
112
113***Submission Instructions***
114
115See syllabus.
116
117
118***Important***
119
120Be creative.  When something is unclear make a reasonable assumption.
121
122Also, do not try to cover everything at once; start with small
123examples and then expand from there.
124
125
126***Example program***
127
128program P
129    const int size = 10;
130    enum Num { ZERO, ONE, TEN = 10 }
131    interface I {
132        int getp(int i);
133        int getn(int i);
134    }
135
136    class Table implements I {
137        int pos[], neg[];
138        {
139            void  putp(int a, int idx) { this.pos[idx]=a; }
140            void  putn (int a, int idx) { this.neg[idx]=a; }
141            int  getp (int idx) { return pos[idx]; }
142            int  getn (int idx) { return neg[idx]; }
143        }
144    }
145
146    Table val;
147{
148    void f(char ch, int a, int arg)
149        int x;
150        {
151            x = arg;
152        }
153
154    void main()
155        int x, i;
156        char c;
157    {
158        //---------- Initialize val
159        val = new Table;
160        val.pos = new int [size];
161        val.neg = new int [size];
162        for (i = 0; i<size; i++)
163        {
164            val.putp(0,i);
165            val.putn(0,i);
166        }
167        f(c, x, i);
168
169        //---------- Read values
170        read(x);
171        for(;x > 0;)
172        {
173            if (Num.ZERO<= x && x < size)
174            {
175                val.putp(val.getp(x)+Num.ONE);
176            } else
177            {
178                if (-size < x && x < 0)
179                {
180                    val.putn(val.getn(-x)+1);
181                }
182            }
183            read(x);
184        }
185    }
186}