CompSci 142 / CSE 142 Winter 2018 | News | Course Reference | Schedule | Project Guide
This webpage was adapted from Alex Thornton’s offering of CS 141


CompSci 142 / CSE 142 Winter 2018
Crux Language Specification


Lexical Semantics

A program written in Crux consists of a sequence of lexemes, each of which can be classified as a kind of token. The kinds of tokens, and the rules that govern their appearance are as follows:

 

·  As in Java, comments begin with a double forward slash and continue until the end of the line on which they appear. Comments should be ignored by the scanner, because they do not constitute a lexeme.

·  Whitespace should be ignored, as it does not constitute a lexeme.

·  The following words are reserved types, but are recognized as IDENTIFIER tokens: void, bool, int, float.

·  The following words are reserved keywords:

 

Name

Lexeme

AND

and

OR

or

NOT

not

LET

let

VAR

var

ARRAY

array

FUNC

func

IF

if

ELSE

else

WHILE

while

TRUE

true

FALSE

false

RETURN

return

 

·  The following character sequences have special meaning:

 

Name

Lexeme

OPEN_PAREN

(

CLOSE_PAREN

)

OPEN_BRACE

{

CLOSE_BRACE

}

OPEN_BRACKET

[

CLOSE_BRACKET

]

ADD

+

SUB

-

MUL

*

DIV

/

GREATER_EQUAL

>=

LESSER_EQUAL

<=

NOT_EQUAL

!=

EQUAL

==

GREATER_THAN

> 

LESS_THAN

< 

ASSIGN

=

COMMA

,

SEMICOLON

;

COLON

:

CALL

::

 

·  The following patterns are reserved value literals:

Name

Lexeme Pattern

INTEGER

digit {digit}

FLOAT

digit {digit} "." {digit}

IDENTIFIER

("_" | letter) { "_" | letter | digit }

 

where

 

digit := "0" | "1" | ... | "9" .

lowercase-letter := "a" | "b" | ... | "z" .

uppercase-letter := "A" | "B" | ... | "Z" .

letter := lowercase-letter | uppercase-letter .

 

·  The following special circumstances generate special tokens:

Name

Circumstance

ERROR

Any character sequence not otherwise reserved. For example, a "!" not followed by an "=".

EOF

The end-of-file marker.

 

 


Crux Grammar

The crux grammar is given in Wirth Syntax Notation.

literal := INTEGER | FLOAT | TRUE | FALSE .
 
designator := IDENTIFIER { "[" expression0 "]" } .
type := IDENTIFIER .
 
op0 := ">=" | "<=" | "!=" | "==" | ">" | "<" .
op1 := "+" | "-" | "or" .
op2 := "*" | "/" | "and" .
 
expression0 := expression1 [ op0 expression1 ] .
expression1 := expression2 { op1  expression2 } .
expression2 := expression3 { op2 expression3 } .
expression3 := "not" expression3
       | "(" expression0 ")"
       | designator
       | call-expression
       | literal .
call-expression := "::" IDENTIFIER "(" expression-list ")" .
expression-list := [ expression0 { "," expression0 } ] .
 
parameter := IDENTIFIER ":" type .
parameter-list := [ parameter { "," parameter } ] .
 
variable-declaration := "var" IDENTIFIER ":" type ";" .
array-declaration := "array" IDENTIFIER ":" type "[" INTEGER "]" { "[" INTEGER "]" } ";" .
function-definition := "func" IDENTIFIER "(" parameter-list ")" ":" type statement-block .
declaration := variable-declaration | array-declaration | function-definition .
declaration-list := { declaration } .
 
assignment-statement := "let" designator "=" expression0 ";" .
call-statement := call-expression ";" .
if-statement := "if" expression0 statement-block [ "else" statement-block ] .
while-statement := "while" expression0 statement-block .
return-statement := "return" expression0 ";" .
statement := variable-declaration
           | call-statement
           | assignment-statement
           | if-statement
           | while-statement
           | return-statement .
statement-list := { statement } .
statement-block := "{" statement-list "}" .
 
program := declaration-list EOF .
    

Pre-defined Functions

  • readInt() : int, Prompts the user for an integer.
  • readFloat() : float, Prompts the user for an integer.
  • printBool(arg:bool) : void, Prints a bool value to the screen.
  • printInt(arg:int) : void, Prints an integer value to the screen.
  • printFloat(arg:float) : void, Prints a float value to the screen.
  • println() : void, Prints newline character to the screen.

 

Runtime Constraints

 

All valid crux programs have one function with the signature: main() : void. This function represents the starting point of the crux program.


Symbol Semantics


Type Semantics


·         Language derived from Eric Hennigan’s CS 142, Fall 2013.

·         Page adapted from a similar document from CS 141 Winter 2013 by Alex Thornton.