The parser builds on the Dana lexer and implements the full grammar of the Dana language, including automatic layout handling. It validates source programs syntactically and reports detailed errors with line/column positions and a caret indicator.
- Full grammar of Dana with layout rules.
- Detailed syntax errors with line/column and caret.
- Reports both the unexpected token and the expected tokens.
- Stable exit codes (
0on success, non-zero on error).
- bison (GNU Bison 3.0+)
- flex (from the lexer stage)
- make
- A POSIX shell (Linux / macOS)
From this directory:
make # build parser
make clean # remove generated .cpp/.o files
make distclean # remove all generated files, including the parser binaryThe build produces the binary: ./parser.
Usage: ./parser [OPTIONS] [FILE]
Parse input from FILE, or standard input if FILE is omitted or '-'.
Options:
-h, --help Show this help and exit
Examples:
./parser program.dana # Parse a file
./parser - # Parse from stdin
./parser < program.dana # Equivalent: parse from stdinIf the source is syntactically valid, the parser prints:
Parsing completed successfully
and exits with code 0 (see Exit Codes).
On syntax errors, the parser reports:
- The line and column number,
- A caret (
^) pointing at the exact position, - The unexpected token,
- And what was expected (if applicable).
Input
def hello (writeInteger)
writeString: "Hello world!\n"
Command
./parser hello.danaOutput
Syntax error at line 1, column 12
1: def hello (writeInteger)
^
Unexpected token: '('
Input
# Error: 'elif' used without a preceding 'if'
def main
begin
elif condition
writeString: "This should never be reached"
end
Command
./parser main.danaOutput
Syntax error at line 5, column 10
5: elif condition
^
Unexpected token: elif
0— success (valid syntax)- non-zero — syntax error (message printed to stderr)
Use the repository’s Python test harness:
cd ../testing
python3 test_parser.pyThe script:
- Runs
.danaprograms through the parser. - Includes both valid and invalid (in the
testing/invalid_parser directory) programs. - Checks that valid programs exit with
0and invalid ones fail with an error. - Prints the parser’s error output for failing tests.
-
Ensure you’ve read and build the lexer first.
-
If you see
bison: command not found, install it (along with flex):sudo apt-get install bison flex