Commit b90da5cf authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆
Browse files

fix indentation

parent 8a374aed
......@@ -54,12 +54,12 @@ HelloContext tree = parser.hello();
You can easily examine the token stream by asking for a list of tokens. First, you must make sure that all the input has been processed, by calling `fill()`. Tthe parser will normally read tokens one by one (possibly looking ahead a few tokens), so the lexer produces tokens on demand – `fill()` makes it finish the job.
```java
// process all the input
tokens.fill();
// process all the input
tokens.fill();
// look at each token
for (Token t : tokens.getTokens())
System.out.printf("%-10s (%s)%n", t.getText(), HelloLexer.VOCABULARY.getDisplayName(t.getType()));
// look at each token
for (Token t : tokens.getTokens())
System.out.printf("%-10s (%s)%n", t.getText(), HelloLexer.VOCABULARY.getDisplayName(t.getType()));
```
Given the input `hello world!`, the output should look like this:
......@@ -74,9 +74,9 @@ world (ID)
Each `Token` contains information about its type, it source (e.g., filename), start/end offset, and line number and column of the first character:
```java
for (Token t : tokens.getTokens())
System.out.printf("%-10s #%d, offset=%2d–%2d, line=%d, column=%2d, source=%s%n", t.getText(), t.getTokenIndex(),
t.getStartIndex(), t.getStopIndex(), t.getLine(), t.getCharPositionInLine(), t.getTokenSource().getSourceName());
for (Token t : tokens.getTokens())
System.out.printf("%-10s #%d, offset=%2d–%2d, line=%d, column=%2d, source=%s%n", t.getText(), t.getTokenIndex(),
t.getStartIndex(), t.getStopIndex(), t.getLine(), t.getCharPositionInLine(), t.getTokenSource().getSourceName());
```
E.g.:
......@@ -93,13 +93,13 @@ The token stream itself gives you enough information to do very simple syntax hi
To see the parse result, we can use a `ParseTreeWalker` to visit all the nodes in the parse tree, giving it a listener that will be called for each node:
```java
walker.walk(new HelloBaseListener() {
@Override
public void visitTerminal(TerminalNode node) {
System.out.println("'" + node + "' ");
}
// you can also add visit methods for error nodes, and before and after a non-terminal
}, tree);
walker.walk(new HelloBaseListener() {
@Override
public void visitTerminal(TerminalNode node) {
System.out.println("'" + node + "' ");
}
// you can also add visit methods for error nodes, and before and after a non-terminal
}, tree);
```
The output should look like this (for input `hello world!`):
......@@ -113,12 +113,12 @@ The output should look like this (for input `hello world!`):
A more interesting walker would pick out who we're saying hello to:
```java
new ParseTreeWalker().walk(new HelloBaseListener() {
@Override
public void enterHello(HelloContext ctx) {
System.out.print("Saying hello to '" + ctx.getChild(1) + "'!");
}
}, tree);
new ParseTreeWalker().walk(new HelloBaseListener() {
@Override
public void enterHello(HelloContext ctx) {
System.out.print("Saying hello to '" + ctx.getChild(1) + "'!");
}
}, tree);
```
Giving the output `Saying hello to 'world'!`
......@@ -150,28 +150,28 @@ If you're setting up / adding ANTLR4 to your own project, you can cut and paste
* The ANTLR4 runtime is needed to run the compiled parser; add it in the `<depencencies>…</dependencies>` section:
```xml
<!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime -->
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr4.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime -->
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr4.version}</version>
</dependency>
```
* The ANTLR4 maven plugin includes the ANTLR4 tool, and is needed to generate parser during compilation; add it to `<build><plugins>…</plugins></build>`:
```xml
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4.version}</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4.version}</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
```
......@@ -11,8 +11,7 @@
<name>INF225 ANTLR Example</name>
<description>A simple ANTLR v4 example.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<url>https://retting.ii.uib.no/inf225.h20/antlr-example</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment