This is an old revision of the document!


Homework 3 - Alf Language

Information

Deadline: 15th of April, 23:55
Points: 2 points out of the final grade
Upload the homework: vmchecker.cs.pub.ro
Late upload: 0.1 points / day (maximum 4 days)

Request

Parse the ALF language source file and generate a JSON abstract syntax tree (AST).

The homework will take one or two parameters from the command line. The first paremeter is the filename with the Alf script, the second one the output file. If the output file is missing, the output is the same as the script file with the extension ”.json”.

node main.js script.alf script.alf.json

Tips

Here are some tips for solving the homework:

  • start with writing the parser rules for the values
  • add the parser rules for expressions
  • add the parser rules for the variable definitions
  • add the parser rules for the loops
  • add the parser rules for the function definitions
  • add the parser rules for the function run
  • add the parser rules for the branches and loops
  • add the parser rules for the array and struct

Try running small scripts to test every feature.

Syntax highlighting

If you use sublime text, you may use this syntax highlighting file to write Alf language.

You may read here how to add the file.

AST Format

All the nodes in the AST have the following format:

{
  id:"node_id"
}

The list of the node ids is:

  • module - a module (the actual program)
  • var - variable definition
  • exp - any expression
  • set - setting the value of a variable
  • message - message definition
  • class - call definition
  • property - a property of a class
  • list - list definition
  • element - an element of an array
  • if - if statement
  • while - while statement
  • repeat - repeat statement
  • for - for statement
  • dispatch - send a message
  • return - value of a function
  • value - a value (int, real, logic, float, user_defined)

Questions

If you have any questions related to the homework, please ask them by posting an issue on the github repository with the title format [calculator] <your question title>. You will need a github account for that.

DO NO POST ANY CODE. This is considered copying and will result in a 0p homework for you.

If you want to receive an email when issues are posted or when there are new messages, got to the github repository and click Watch.

ALF

The Alf language is a typed language defined as follows:

  • variable definitions
  • message definitions
  • class definitions (new data type)
  • list definitions (new data type)
  • expressions
  • values
  • attributions
  • message sending
  • branch (if)
  • loop (while , for or repeat)

The instructions are separated by newline.

a:=10
b:=20l
s:=55

Notations

  • [optional] - anything between [] is an optional part

Values

Values are

  • integer number
    7
  • real number
    7.5
  • character
    "a"
  • string
    "this is a text"
  • logic (value true or false)
    false
  • the null value
    none

Strings may have the following special characters:

  • ” - denoted \”
  • \ - denoted \\
  • \r - denoted \r
  • \n - denoted \n

Comments

Comments are a part of the source file that is ignored. They are enclosed between ”{” and ”}” and are not imbricated.

Comments might span across several lines.

All the test files have on comment on the first line. If your grammar has no rules for comments, just skip the first line.

{this is a text to describe the script}
define v:=55

Identifier

An identifier can be any value that starts with a letter, $ or _ and may have also numbers in its name.

Language

identifier
$identifier
_identifier

AST

{
	id:"identifier",
 	title:"identifier_name"
}

Value

A value is any integer number, float number, logic or string.

Language

3534
45634.423
false
"s"
"string"

AST

{
 	id:"value",
	type:"value_type", // integer, real, character, logic, string
 	value:actual_value
}

Example

Variable definition

Language

define variable_name [:variable_type] [:= value or expression], ...

Examples

define a:integer := 3
define a := 3
define a1:integer:=3, a2:string := "text", a3:int
Data Types
  • integer - integer number
  • real - floating point number
  • character - a single character
  • string - text
  • logic - true or false
  • user defined (class, list)

AST

{
	id:"var",
 	variables:[ // array of variables
 		{
 			type:"data type",
  			title: "varable name"
 		},
	]
}

Example

Class

Language

class class_name [extends super_class]
	property property_name:data_type [:= initial_value]
	...
	...
        message message_name[:data_type] parameter1[:data_type][:=default_value], parameter1[:data_type][:=default_value], parameter3[:data_type][:=default_value], ...
        begin
           // ... message source
        end
end;

AST

{
	id:"class",
	title:"class_name",
        parent:"super_class_name",
 	properties:[ // array of properties
 		{
 			type:"property data type",
 			title: "property name",
 			value: "property value" // if it exists
 		},
	],
        messages: [ // array of messages
                {
        		type:"message return data type"
        		title:"message_name",      
			parameters:[ // array of parameters
 				{
 					type:"data type",
 					title: "parameter name"
 				},
 			],
			statements:[ // array of statements (even if it is only one)
				...
 			]
                }
}

Example

Property access

Language
object_name|property
AST
{
	id:"property",
	object: {
		expression for object
	},
 	title: {
		expression for property
	}
}

Example

Array

List definition

Language
list_name:elements_data_type[integer_number to integer_number]
<note>
[] are actual brackets and are mandatory
</note>
AST
{
	id:"list",
        title: array_name,
	element_type:elements_data_type,
 	from: from_integer_number,
 	to: to_integer_number
}

List access

Language
array_name#index
AST
{
	id:"element_of_list",
	list:{
		expression for list
	},
 	index: {
		expression for index
	}
}

Example

Expressions

Operator precedence

Operator Precedence
if Low
= !=
> >= < ⇐
and or xor
not
+ -
* / mod High

Language

2+5
2*4+5
variable+5
x>y
x=y

2+(x+y)*(5+6)

AST

{
	id:"exp",
	op:"operator",
 	left:left_operand,
 	right:right_operand
}

Operator '-' has a different AST if it is used as un unary (ex: -50)

{
	id:"exp",
	op:"negative",
 	value:50
}

Attribution

Language

variable := expression;

Examples

x := 1000
s := "text" + "s"
y := [module f]
l#i := 900
q.e := "this is a text"

AST

{
	id:"set",
 	to: {
 	 	... id, property, element_of_list
 	},
 	from: {
 	 	... exp
 	}
}

Example

Message definition

Messages are functions and are usually defined in classes. If a message is defined outside a class, it belongs to the module object.

Language

message message_name:return_type parameter1:[parameter1_type][:=value], ... = expression
message message_name:return_type parameter1:[parameter1_type][:=value], ... 
begin
   ...
end

Examples

message f1
begin
end;
message sum:int a:int, b:int = is a+b
message power:int a:int, n:int 
begin
  define p:int
  define i:int
  if n = 0 then 
  begin
     power:=1
  end
  for i in [1,n] p:=p * a
  return p
end

AST

{
	id:"message",
	title:"message_name",      
	parameters:[ // array of parameters
 		{
 			type:"data type",
 			id: "parameter name"
 		},
 	],
	return_type:"type of the value that the message returns",
	statements:[ // array of statements (even if it is only one)
		...
 	]
}

Example

Return

This is the statements that sets the return value of the message.

Example
return x+y
AST
{
	id:"return",
	value: {
		...
	}
}

Message dispatch

The is a call for a function

Language

[object message_name parameter_name1:value1, parameter_name2:value, ...]
[object message_name value1, value2, ...]

Examples

[module write text:"Text"]
s := [sum n1:3 n2:5]
[module getdir]

AST

{
	id:"dispatch",
	message:"message_name",
	object:"destination_object_name",
	parameters:{ // dictionary of parameter name or index and value
 		"parameter1": parameter_value,
 		"2":parameter value,
 		...
 	}
}

Example

Branch

Language

if expression
  ... 
end
if expression 
  ... 
else
  ...
end

AST

{
	id:"if",
	exp:expression,
	then: [ // list of statements 
	]
}
{
	id:"if",
	exp:expression,
	then: [ // list of statements 
	],
	else: [ // list of statements 
	]
}

Example

Loop

There are three types of loops: while, repeat and for.

while

while expression
  ...
end
AST
{
	id:"while",
	exp :expression,
	statements: [ // array of statements
 
	]
}

repeat

repeat 
  ...
when expression
AST
{
	id:"repeat",
	exp :expression,
	statements: [ // array of statements
 
	]
}

for

for variable_name in expression 
   ...
end

or

for variable_name in (number to number)
   ...
end
for variable_name in (number downto number)
   ...
end
AST
{
	id:"for",
        variable:variable_name,
	exp:{
		expression 
		...
	}
	statements: [ // array of statements
 
	]
}

or

{
	id:"for",
        variable:variable_name,
	list:{
		low: { expression },
		high: { expression },
		direction: "to" or "downto"
	},
	statements: [ // array of statements
 
	]
}

Example

Errors

If the source file has any error, you will have to output a JSON with the error. There are two kinds of errors:

  • lexical (unrecognised token)
  • syntax (unexpected token)

Lexical

For any lexical error, the output will be:

{
  error:"lexical",
  line: line_number,
  text: "text that is not recognised"
}

Line numbers start with 1

Syntax

For any syntactical error, the output will be:

{
  error:"syntax",
  line: line_number,
  token: "token that the parser saw", // token name will not be checked by checker 
  expected: [list of expected tokens] // token name will not be checked by checker
}

Token names will be different for each of you, they will be ignored by the checker. It will only check that you have some token in the token item and in the list.

Line numbers start with 1

Bonus

For an additional 0.5p, implement the following:

Add preprocessing to the source. You have

  • register
  • if then / else / endif
  • unregister

register

Registers an identifier to a value.

#register identifier value

The identifier will be replaced in the program with the value (textually).

if

Verifies if an identifier is registered and has a value and adds the source code up to else or endif. Else is optional.

#register platform ...

...

#if platform = windows 
p := "Windows"  {this source is ignored if platform is not windows}
#else
p := "Linux"  {this source code is ignored if platform is not linux}
end

unregister

Unregister an identifier from a value.

#register N value

...

#unregister N

{N is undefined}

All the preproessing tags are on a separate line

Bonus will be awarded only if all other tests pass.

Testing

The homework will be tested automatically using a set of public and private tests.

Public Tests

You can download the public tests from the GitHub repository.

To run the tests, download the contents of the repository in the folder with the homework. Enter the verify folder and run ./run_all.sh.

cd verify
./run_all.sh

You will need bash for that. You can use either Linux or Windows Linux Subsystem.

To install nodejs in Linux or Windows Linux Subsystem, do the following:

wget https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz
tar xvfJ node-v6.10.0-linux-x64.tar.xz
cd node-v6.10.0-linux-x64
sudo cp -R * /usr

Private Tests

When uploading the homework, we might have some private tests that it needs to pass. vmchecker will run them.

You may always upload the homework as many times you want until the deadline. This will run all the tests for you and display the result.

Upload the homework

The homework needs to be uploaded to vmchecker. Login with your moodle user name, select the Automates et Langages Formelles (FILS) course and upload the homework archive.

Readme

The readme file has the following format:

Your full name
Group

An explanation how you wrote your homework, what did you use, what are the main ideas.

Homework Archive

To upload your homework, please follow the following:

  1. Create a zip (not rar, ace, 7zip or anything else) archive containing:
    • your main file (set in package.json)
    • your javascript files (*.js)
    • your jison file (grammar.jison)
    • the package.json file
    • the Readme file
  2. sign in with vmchecker
  3. select the Automates et Langages Formelles (FILS) course
  4. select 3. Alf Language
  5. upload the archive

The archive needs to contain the files in its root, not in a folder. DO NOT archive a folder with those file, archive DIRECTLY those files.

DO NO include node_modules.

When the archive is uploaded, vmchecker will run:

unzip archive.zip homework
cd homework
npm install
echo '{ "node":true, "loopfunc": true, "esnext":true }' > .jshintrc
jshint *.js
jison grammar.jison -o grammar.js
alf/teme/tema3_en_draft.1522080006.txt.gz · Last modified: 2018/03/26 19:00 by alexandru.radovici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0