Skip to content

Latest commit

 

History

History
155 lines (107 loc) · 3.02 KB

File metadata and controls

155 lines (107 loc) · 3.02 KB

Dana Parser

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.

Features

  • 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 (0 on success, non-zero on error).

Prerequisites

  • bison (GNU Bison 3.0+)
  • flex (from the lexer stage)
  • make
  • A POSIX shell (Linux / macOS)

Build

From this directory:

make            # build parser
make clean      # remove generated .cpp/.o files
make distclean  # remove all generated files, including the parser binary

The build produces the binary: ./parser.

Usage

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 stdin

Successful Parse Output

If the source is syntactically valid, the parser prints:

Parsing completed successfully

and exits with code 0 (see Exit Codes).

Error Reporting

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).

Example 1 — Unexpected Operator

Input

def hello (writeInteger)
  writeString: "Hello world!\n"

Command

./parser hello.dana

Output

Syntax error at line 1, column 12
1:  def hello (writeInteger)
              ^
Unexpected token: '('

Example 2 — elif Without if

Input

# Error: 'elif' used without a preceding 'if'
def main
    begin
        elif condition
            writeString: "This should never be reached"
    end

Command

./parser main.dana

Output

Syntax error at line 5, column 10
5:          elif condition
            ^
Unexpected token: elif

Exit Codes

  • 0 — success (valid syntax)
  • non-zero — syntax error (message printed to stderr)

Testing

Use the repository’s Python test harness:

cd ../testing
python3 test_parser.py

The script:

  • Runs .dana programs through the parser.
  • Includes both valid and invalid (in the testing/invalid_parser directory) programs.
  • Checks that valid programs exit with 0 and invalid ones fail with an error.
  • Prints the parser’s error output for failing tests.

Tips & Troubleshooting

  • 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