{ /* cll.syn -- Syntax for the baseline C-like language used by XIDEK: the Extensible Script Language Development Kit Copyright (c) 2002 Parsifal Software. All Rights Reserved. This file represents the underlying syntax used to describe the scripts interpreted by the baseline cll examples in the kit. It is easier to read and understand than the actual .syn files because reduction procedures and type declarations for tokens on the left side of productions have been stripped, along with some comments, configuration parameters, and definitions which pertain to individual examples. .syn files are compiled with the AnaGram parser generator, which creates a corresponding parser file and header file. This grammar describes a language that is similar to C. The language consists of C statements (not including declarations or the switch statement) and C expressions (not including pointers or subscript operators). The language also implements Fortran-style exponentiation and simple dump and print statements. The syntax below uses "open" and "closed" statements to eliminate the traditional "dangling else" or "if-else" ambiguity problem. See http://www.parsifalsoft.com/ifelse.html or doc/ifelse.htm for a more detailed discussion. Statement types supported are: expression statements compound statements if/else statements while statements do/while statements for statements break and continue statements dump and print statements There are no declarations. Scalar values may be explicitly cast to (long) or (double). Both integer and floating point values are stored as doubles. Scripts may contain any number of statements. White space may be used freely, including both C and C++ style comments. For further information about this program or the AnaGram parser generator, please contact: Parsifal Software http://www.parsifalsoft.com info@parsifalsoft.com +1-800-879-2577, Voice/Fax +1-508-358-2564 P.O. Box 219 Wayland, Massachusetts 01778 USA */ #include "comdefs.h" // Contains definitions of support classes #include } /*** CONFIGURATION SECTION **************************************************/ [ // Grammar adjustments disregard white space // Skip over white space (defined below) distinguish lexemes lexeme {integer, real, name, string element, character constant} distinguish keywords {'a-z' + 'A-Z'} wrapper {AgString, Value} // Special handling on parser stack parser file name = "#.cpp" // # will be replaced by name of syntax file test file mask = "*.scf" // filter for File Trace no cr //omit carriage returns in output files, for *nix compatibility // Operating modes pointer input // Take input from array in memory pointer type = const unsigned char * reentrant parser // Make parser reentrant ] /*** GRAMMAR ****************************************************************/ /* "script" is marked with a $ to indicate it is the "grammar token", that is, the whole of the input we're intending to parse. */ script $ -> statement list, eof // Zero or more statements. Note that "statement list" is recursively defined. statement list -> -> statement list, statement /* A single statement can be "open" or "closed". An "open" statement is a statement that can legally be followed by an "else" keyword. A "closed" statement is one that cannot. "Open" and "closed" statements are a means for resolving a problem common to many programming languages known as the if-then-else ambiguity or "dangling else". See www.parsifalsoft.com/ifelse.htm for a discussion of the ambiguity and this technique for resolving it. */ statement -> open statement -> closed statement open statement -> if condition, statement -> if condition, closed statement, "else", open statement -> WHILE, '(', expression, ')', open statement -> FOR, '(', optional expression, ';', expression, ';', optional expression, ')', open statement closed statement -> if condition, closed statement, "else", closed statement -> WHILE, '(', expression, ')', closed statement -> FOR, '(', optional expression, ';', expression, ';', optional expression, ')', closed statement -> simple statement /* A "simple statement" is one that does not end with another statement. All simple statements are closed statements and are therefore factored out for clarity. */ simple statement -> DO, statement, "while", '(', expression, ')', ';' -> expression, ';' -> ';' -> compound statement -> "break", ';' -> "continue", ';' -> "return", optional expression, ';' -> dump statement, ';' -> print statement, ';' DO -> "do" WHILE -> "while" FOR -> "for" if condition -> "if", '(', expression, ')' compound statement -> '{', statement list, '}' // The dump statement accepts a comma delimited list of variable names dump statement -> "dump", name -> dump statement, ',', name // The print statement accepts a comma delimited list of expressions print statement -> "print", print expression -> print statement, ',', print expression print expression -> assignment expression -> assignment expression, "in", '(', unit expression, ')' optional expression -> -> expression // General expression. As in C, comma expression has the lowest precedence. expression -> assignment expression // next higher precedence level -> expression, ',', assignment expression // Assignment expression assignment expression -> conditional expression // next higher precedence level -> lvalue, assignment op, assignment expression // Conditional expression (ternary operator) conditional expression -> logical or expression // next higher precedence level -> logical or expression, '?', expression, ':', conditional expression // Logical expressions with short-cut evaluation, as in C. logical or expression -> logical and expression // next higher precedence level -> logical and expression, "||", logical or expression logical and expression -> inclusive or expression // next higher precedence level -> inclusive or expression, "&&", logical and expression // Arithmetic expressions, in operator precedence order. inclusive or expression -> exclusive or expression // next higher precedence level -> inclusive or expression, '|', exclusive or expression exclusive or expression -> and expression // next higher precedence level -> exclusive or expression, '^', and expression and expression -> equality expression // next higher precedence level -> and expression, '&', equality expression equality expression -> relational expression // next higher precedence level -> equality expression, equality op, relational expression relational expression -> shift expression // next higher precedence level -> relational expression, relational op, shift expression shift expression -> additive expression // next higher precedence level -> shift expression, shift op, additive expression additive expression -> multiplicative expression // next higher precedence level -> additive expression, additive op, multiplicative expression multiplicative expression -> unary expression // next higher precedence level -> multiplicative expression, multiplicative op, unary expression unary expression -> factor // next higher precedence level -> '+', unary expression -> unary op, unary expression /* Syntactically, we can use ** for exponentiation because we don't have pointers. (In C, ** could be confused with pointer indirection.) */ factor -> primary // next higher precedence level -> primary, "**", unary expression /* Primary expression - bottom level of expression syntax. Variable references, constants, the builtin functions. Also, another expression in parentheses. (This is how you make parentheses work the way they're supposed to.) */ primary -> '(', expression, ')' -> constant -> numeric constant, '(', unit expression, ')' -> lvalue -> "++", lvalue -> "--", lvalue -> lvalue, "++" -> lvalue, "--" -> function call -> '(', "long", ')', primary -> '(', "double", ')', primary unit expression -> name -> unit expression, '*', name -> unit expression, '/', name lvalue -> name function call -> name, '(', optional arg list, ')' optional arg list -> -> arg list arg list -> assignment expression -> arg list, ',', assignment expression numeric constant -> integer -> real constant -> integer -> real -> string -> character constant // Operator definitions assignment op -> '=' -> "+=" -> "-=" -> "*=" -> "/=" -> "%=" -> "|=" -> "&=" -> "^=" -> "<<=" -> ">>=" -> "**=" equality op -> "==" -> "!=" relational op -> '<' -> "<=" -> '>' -> ">=" shift op -> "<<" -> ">>" additive op -> '+' -> '-' multiplicative op -> '*' -> '/' -> '%' unary op -> '-' -> '!' -> '~' /*** LEXICAL UNITS **********************************************************/ digit = '0-9' eof = 0 // string null terminator letter = 'a-z' + 'A-Z' + '_' space = ' ' + '\t' + '\f' + '\v' + '\r' + '\n' // blank, tab, etc. (void) white space -> space -> "/*", ~eof?..., "*/" // C style comment -> "//", ~(eof+'\n')?..., '\n' // C++ style comment /* Identifying variable names Characters in a name are accumulated in an AgString structure. */ (AgString) name -> letter:c =AgString().append(c); -> name:ns, letter+digit:c =ns.append(c); // Parsing and evaluating numeric constants (double) real -> simple real -> simple real:x, 'e'+'E', signed exponent:e =x*pow(10,e); -> integer part:x, 'e'+'E', signed exponent:e =x*pow(10,e); (double) simple real -> integer part:i, '.', fraction part:f =i+f; -> integer part:i, '.' =i; -> '.', fraction part:f =f; (double) integer part -> decimal integer:x =x; -> hybrid integer:x =x; -> octal integer:x =makeDecimal(x); (long) signed exponent -> '+'?, exponent:x =(long)x; -> '-', exponent:x =-(long)x; (double) fraction part -> digit:d =(d-'0')/10.0; -> digit:d, fraction part:f =(d-'0' + f)/10.0; (long) integer -> decimal integer -> octal integer -> hex integer (long) decimal integer -> '1-9':d =d-'0'; -> decimal integer:x, digit:d =10*x + d-'0'; (long) exponent -> '0-9':d =d-'0'; -> exponent:x, digit:d =10*x + d-'0'; (long) hybrid integer -> octal integer:x, '8-9':d =10*makeDecimal(x) + d - '0'; -> hybrid integer:x, digit:d =10*x + d-'0'; (long) octal integer -> '0' =0; -> octal integer:n, '0-7':d =8*n + d - '0'; (long) hex integer -> {"0x" | "0X"} =0; -> hex integer:n, hex digit:d =16*n + d; (int) hex digit -> '0-9':x =x - '0'; -> 'a-f' + 'A-F':d =(d & 7) + 9; // string constant (Value) string -> string element -> string:s, string element:e =s+=e; (Value) string element -> '"', s char sequence:b, '"' =Value(b); (AgString) s char sequence -> =AgString(); -> s char sequence:s, s char:c =s.append(c); (int) s char -> ~(eof + '"' + '\n' + '\\') -> escape sequence (int) escape sequence -> simple escape sequence -> octal escape sequence -> hexadecimal escape sequence (int) simple escape sequence -> "\\'" ='\''; -> "\\\"" ='"'; -> "\\?" = '\?'; -> "\\\\" ='\\'; -> "\\a" ='\a'; -> "\\b" ='\b'; -> "\\f" ='\f'; -> "\\n" ='\n'; -> "\\r" ='\r'; -> "\\t" ='\t'; -> "\\v" ='\v'; (int) octal escape sequence -> one octal | two octal | three octal (int) one octal -> '\\', '0-7':d =d-'0'; (int) two octal -> one octal:n, '0-7':d =8*n + d-'0'; (int) three octal -> two octal:n, '0-7':d =8*n + d-'0'; (int) hexadecimal escape sequence -> "\\x", hexadecimal digit:d =d; -> hexadecimal escape sequence:n, hexadecimal digit:d =16*n + d; (int) hexadecimal digit -> '0-9':d =d-'0'; -> 'A-F' + 'a-f':d =9 + (d & 7); [ sticky {one octal, two octal, hexadecimal escape sequence} ] // character constant (int) character constant -> '\'', c char:c, '\'' =c; (int) c char -> ~(eof + '\'' + '\n' + '\\') -> escape sequence /*** End of syntax file *****************************************************/