Gruppen 2 und 4
Samuel Teuber
propa@teuber.dev
https://teuber.dev/propa
$SLL(1)$-Grammatik:
$$
S \to L \underline{=} R \mid R\\
First_1\left(L\underline{=}R\right) \cap First_1\left(R\right) = \left\{\underline{*},\underline{=}\right\} \neq \emptyset
$$
$ Value\ \ \ \ \ \ \to \underline{string} \mid \underline{number} \mid Object\\ Object\ \ \ \ \ \to \underline{\{} Member \underline{\}}\\ Member\ \ \to Pair\ Members'\\ Members\ \to \underline{,} Members \mid \epsilon\\ Pair\ \ \ \ \ \ \ \ \ \to \underline{string} \underline{:} Value $
Alternative für $Member$: $$ Member \to Pair \mid Member Pair $$
Problem: $$ First_k\left(Pair\ Pair\ Pair \dots \right) \subseteq First_k\left(Pair\ Follow_k\left(Member\right)\right)\\ First_k\left(Pair\ Pair\ Pair \dots \right) \subseteq First_k\left(Member\ Pair\ Follow_k\left(Member\right)\right) $$
$ Value\ \ \ \ \ \ \to \underline{string} \mid \underline{number} \mid Object\\ Object\ \ \ \ \ \to \underline{\{} Member \underline{\}}\\ Member\ \ \to Pair\ Members'\\ Members\ \to \underline{,} Members \mid \epsilon\\ Pair\ \ \ \ \ \ \ \ \ \to \underline{string} \underline{:} Value $
JSONValue parseValue() {
String s;
switch (lexer.current.type) {
case STRING:
s = lexer.current.text; lexer.lex();
return new JSONString(s);
case NUMBER:
s = lexer.current.text; lexer.lex();
return new JSONNumber(s);
case LCURLY:
return parseObject();
default: throw error();
}
}
$ Value\ \ \ \ \ \ \to \underline{string} \mid \underline{number} \mid Object\\ Object\ \ \ \ \ \to \underline{\{} Member \underline{\}}\\ Member\ \ \to Pair\ Members'\\ Members\ \to \underline{,} Members \mid \epsilon\\ Pair\ \ \ \ \ \ \ \ \ \to \underline{string} \underline{:} Value $
JSONObject parseObject() {
switch (lexer.current.type) {
case LCURLY:
lexer.lex();
JSONObject o = new JSONObject(parseMembers());
if (lexer.current.type != TokenType.RCURLY)
error();
lexer.lex();
return o;
default: throw error();
}
}
$ Value\ \ \ \ \ \ \to \underline{string} \mid \underline{number} \mid Object\\ Object\ \ \ \ \ \to \underline{\{} Member \underline{\}}\\ Member\ \ \to Pair\ Members'\\ Members\ \to \underline{,} Members \mid \epsilon\\ Pair\ \ \ \ \ \ \ \ \ \to \underline{string} \underline{:} Value $
List<Pair> parseMembers() {
List<Pair> l = new LinkedList<Pair>();
l.add(parsePair());
while (true) {
switch (lexer.current.type) {
case COMMA:
lexer.lex();
l.add(parsePair());
continue;
case RCURLY: break;
default: throw error();
}
break;
}
return l;
}
$ Value\ \ \ \ \ \ \to \underline{string} \mid \underline{number} \mid Object\\ Object\ \ \ \ \ \to \underline{\{} Member \underline{\}}\\ Member\ \ \to Pair\ Members'\\ Members\ \to \underline{,} Members \mid \epsilon\\ Pair\ \ \ \ \ \ \ \ \ \to \underline{string} \underline{:} Value $
Pair parsePair() {
if (lexer.current.type != TokenType.STRING) error();
JSONString s = new JSONString(lexer.current.text);
lexer.lex();
if (lexer.current.type != TokenType.COLON) error();
lexer.lex();
JSONValue v = parseValue();
return new Pair(s, v);
}
(x + (y * z))-7
- + x * y z 7
x y z * + 7 -
iconst_4
iload_1
imul
iconst_5
iadd
iconst_3
idiv
iconst_2
isub
a * (x - 1) + c
iload_n
, iconst_n
, isub
, imul
, iadd
static
: $p_0$ ist this
invokevirtual
/invokestatic
public classTest {
int bar() {
return foo(42);
}
int foo(int i) {
return i;
}
}
int bar();
aload_0
bipush 42
invokevirtual #2
ireturn
int foo(int);
iload_1
ireturn
if ((a > c) || (c > 0))
a = a - this.f(a * (x - 1) + c);
this
in Variable 0a
-> 1; b
-> 2; c
->3Benötigte Instruktionen:
iload_n
, if_icmpgt <label>
, iconst_n
, goto <label>
,iload_n
, aload_n
, iconst_n
, isub
, imul
, iadd
, isub
, invokevirtual
, goto
???
no_shortcut:
???
then:
???
else:
finish:
Neg (Add (Const 1) (Add (Const 2) (Var 3)))
bipush 2
iload_3
iadd
bipush 1
iadd
ineg
vs
bipush 1
bipush 2
iload 3
iadd
iadd
ineg
Code Generation in Haskell
codegen :: Exp -> [String]
-- geg:
height :: Expr -> Int -- Gibt Höhe von Ausdrucksbaum zurück
codegen (Const value) = [ "bipush " ++ (show value) ]
public void f(int a, int b):
iconst_0
istore_3
2: iload_1
iload_2
if_icmplt 17
iload_1
iload_2
isub
istore_1
iinc 3, 1
goto 2
17: aload_0
iload_3
putfield x:I
aload_0
iload_1
putfield y:I
return