Loading ...
Sorry, an error occurred while loading the content.

newbie question: "indention-aware syntax"

Expand Messages
  • Kaweh Kazemi
    hello! i am new to ANTLR. there was no problem to create a Lexer/Parser for my small scripting language. ANTLR is very nice and clean. here is my problem: C,
    Message 1 of 1 , Sep 30, 1999
    • 0 Attachment
      hello!

      i am new to ANTLR. there was no problem to create a Lexer/Parser for my
      small scripting language. ANTLR is very nice and clean.

      here is my problem:

      C, C++ or Jave use "{" "}" to start/end block, Pascal uses "begin" "end" to
      define blockls. my little scripting languages uses if/end or while/end
      (everything between if/while and end is a new block).

      but now i try to change the syntax to be "indention-aware". what does that
      mean? well, maybe you know Python (www.python.org). Python uses indention to
      define blocks.

      example:

      * C/C++/Java:

      if (condition)
      {
      x = 1;
      y = 1;

      while (x < 100)
      {
      x++;
      }
      }
      z = 0;

      * Python:

      if condition:
      x = 1;
      y = 1;
      while x < 100:
      x = x + 1
      z = 1;

      how does Python do this? following text is taken from Python Language
      Reference:

      "Leading whitespace (spaces and tabs) at the beginning of a logical line is
      used to compute the indentation level of the line, which in turn is used to
      determine the grouping of statements.

      The indentation levels of consecutive lines are used to generate INDENT and
      DEDENT tokens, using a stack, as follows.

      Before the first line of the file is read, a single zero is pushed on the
      stack; this will never be popped off again. The numbers pushed on the stack
      will always be strictly increasing from bottom to top. At the beginning of
      each logical line, the line's indentation level is compared to the top of
      the stack. If it is equal, nothing happens. If it is larger, it is pushed on
      the stack, and one INDENT token is generated. If it is smaller, it must be
      one of the numbers occurring on the stack; all numbers on the stack that are
      larger are popped off, and for each number popped off a DEDENT token is
      generated. At the end of the file, a DEDENT token is generated for each
      number remaining on the stack that is larger than zero."

      now my question: can i create the same behaviour with ANTLR. is there a way
      to realize indention (let's assume ONLY spaces are allowed no TABS) and to
      create INDENT and DEDENT tokens?

      thanks you for any help in advance,
      kaweh (kaweh@...)
    Your message has been successfully submitted and would be delivered to recipients shortly.