143 lines
3.7 KiB
Typst
143 lines
3.7 KiB
Typst
#import "@preview/codly:1.3.0": *
|
||
#import "@preview/codly-languages:0.1.1": *
|
||
#import "@preview/catppuccin:1.0.1": catppuccin, flavors
|
||
|
||
#show: codly-init.with()
|
||
#show: catppuccin.with(flavors.latte)
|
||
|
||
#codly(languages: codly-languages)
|
||
|
||
= Exercise 01
|
||
|
||
#let question(body) = block(
|
||
fill: luma(240),
|
||
inset: 8pt,
|
||
radius: 4pt
|
||
)[
|
||
#body
|
||
]
|
||
|
||
== 01 Grundbegriffe
|
||
|
||
=== Task a
|
||
|
||
#question[
|
||
Wie lang (in Terminalsymbolen) ist der kürzeste Satz in MicroJava, der mindestens ein Tilde (“∼”)
|
||
enthält? Geben Sie ein Beispiel für einen Satz dieser Länge an. Der Satz muss nur syntaktisch korrekt
|
||
(entspricht der Grammatik) sein, nicht semantisch (hält alle Kontextbedingungen der Sprache ein). Bei-
|
||
spielsweise ist x = x + 1, ohne x zuvor zu definieren, eine syntaktisch korrekte Zuweisung, semantisch
|
||
korrekt ist sie aber nicht.
|
||
]
|
||
|
||
```java
|
||
x[~1]++;
|
||
```
|
||
|
||
The statement above contains the following tokens:
|
||
|
||
- x (iden, terminal)
|
||
- "\[" (literal, terminal)
|
||
- "~" (literall, terminal)
|
||
- 1 (number, terminal)
|
||
- "\]" (literal, terminal)
|
||
- "++" (literal, terminal)
|
||
- ";" (literal, terminal)
|
||
|
||
To sum it up the statement has a `~` and has a terminal symbol count of 7.
|
||
|
||
=== Task b
|
||
|
||
#question[
|
||
Sind die Nonterminalsymbole Statement und ActPars rekursiv? Wenn ja, geben Sie alle Rekursionen-
|
||
arten an (Transitivität: direkt, indirekt; Orientierung: links, rechts, zentral). Beispiele für Rekursionsarten
|
||
sind direkt zentralrekursiv, indirekt linksrekursiv, . . . Pro NT kann es mehrere Rekursionsarten geben.
|
||
Geben Sie bei indirekten Rekursionen einen möglichen Pfad an (Beispiel: Factor ist indirekt zentralre-
|
||
kursiv über Factor -> Expr -> Term -> Factor).
|
||
]
|
||
|
||
==== Statement
|
||
|
||
The relevant Productions for the Statement Symbol are:
|
||
|
||
```
|
||
Statement = Designator ( Assignop Expr | ActPars | "++" | "--" ) ";"
|
||
| "if" "(" Condition ")" Statement [ "else" Statement ]
|
||
| "while" "(" Condition ")" Statement
|
||
| "break" ";"
|
||
| "return" [ Expr ] ";"
|
||
| "read" "(" Designator ")" ";"
|
||
| "print" "(" Expr [ "," number ] ")" ";"
|
||
| Block
|
||
| ";".
|
||
|
||
Block = "{" { Statement } "}".
|
||
```
|
||
|
||
We have the following Recursions:
|
||
|
||
- line 2: Statement is directly central recursive
|
||
- line 3: Statement is directly right recursive
|
||
- line 8: Block is indirectly central recursive
|
||
|
||
|
||
==== ActPars
|
||
|
||
The relevant Productions for the ActPars Symbol are:
|
||
|
||
```
|
||
ActPars = "(" [ Expr { "," Expr } ] ")".
|
||
|
||
Expr = [ "–" ] Term { Addop Term }.
|
||
|
||
Term = Factor { Mulop Factor }.
|
||
|
||
Factor = Designator [ ActPars ]
|
||
| number
|
||
| charConst
|
||
| "new" ident [ "[" Expr "]" ]
|
||
| "(" Expr ")".
|
||
```
|
||
|
||
We have the following Recursion:
|
||
|
||
- line 1: Factor is indirectly central recursive
|
||
|
||
=== Task c
|
||
|
||
#question[
|
||
Wie sieht der konkrete Syntaxbaum für folgenden Satz aus? (Achtung, kann groß werden)
|
||
|
||
```java
|
||
program P int i; { void main(){ read(a[~i]); }}
|
||
```
|
||
|
||
- Geben Sie für Symbole der Terminalklassen ident und charConst im Baum auch die zugehörigen Werte an. Beispiel: ident (“main”)
|
||
- Sie dürfen den Syntaxbaum auch handschriftlich erstellen und abfotografieren. Achten Sie dabei bitte darauf, dass die Diagramme sauber gezeichnet und gut lesbar sind.
|
||
- Wenn Sie das Diagramm digital zeichnen wollen, dann bieten sich Zeichentools wie beispielsweise draw.io, LibreOffice Draw oder auch Microsoft PowerPoint an
|
||
|
||
]
|
||
|
||
#figure(
|
||
image("task1c.svg"),
|
||
)
|
||
|
||
=== Task d
|
||
|
||
#question[
|
||
Welche terminalen Anfänge und Nachfolger haben die Regeln MethodDecl, AssignOp und Cond- Term? Bitte die finalen Antworten komplett ausschreiben (i.e., jeweils ein Set an TS)
|
||
]
|
||
|
||
==== First Sets
|
||
|
||
- MethodDecl: { void , Type }
|
||
- AsignOp: { "=" , "+=" , "-=" , "\*=" , "/=" , "%=" }
|
||
- CondTerm: { CondFact }
|
||
|
||
==== Follow Sets
|
||
|
||
- MethodDecl: { "}", "void", Follow(Type) } = { "}", "void", ident }
|
||
- AsignOp: { ";" }
|
||
- CondTerm: { "&&", Follow(CondTerm) } = { "&&", CondFact }
|
||
|
||
|