Homework 3 - Alf Language

Information

Deadline: 7th 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 call
  • 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.

AST Format

All the nodes in the AST have the following format:

{
  id:"node_id",
  line: "the line where the instruction is in the file, starting at 1"
}

All the nodes have id and line.

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
  • function - function definition
  • struct - structure definition
  • property - a property of a class
  • array - array definition
  • element - an element of an array
  • if - if statement
  • loop - while statement
  • for - for statement
  • loop … if - for statement
  • function_call - call (run) a function
  • return - value of a function
  • value - a value (integer, 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 [alf] <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
  • function definitions
  • struct definitions (new data type)
  • array definitions (new data type)
  • expressions
  • values
  • attributions
  • function call
  • branch (if)
  • loop (loop , for)

The instructions are separated by ;.

a=10;
b=20;
s=55;

Notations

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

Values

Values are

  • integer number
    7
  • float number
    7.5
  • symbol
    "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}
def v:integer=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

def variable_name [:variable_type] [= value or expression], ...;
def variable_name, variable_name, variable_name, ...  :variable_type, ....;

Examples

def a:integer = 3;
def a = 3;
def a1:integer = 3, a2:string = "text", a3:integer;
def a,b : integer;
Data Types
  • integer - integer number
  • float - floating point number
  • symbol - a single character
  • string - text
  • logic - true or false
  • user defined (struct, list)

AST

{
	id:"def",
 	variables:[ // array of variables
 		{
 			type:"data type",
  			title: "variable name",
    			value: "variable value"
 		},
	]
}

Example

Sruct

Language

struct struct_name 
	property_name:data_type [= initial_value]
	...
	...
end;

AST

{
	id:"struct",
	title:"struct_name",
 	properties:[ // array of properties
 		{
 			type:"property data type",
 			title: "property name",
 			value: "property value" // if it exists
 		},
	]
}

Example

Property access

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

Example

Array

Array definition

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

Array access

Language
array_name[index]
AST
{
	id:"element_of_array",
	array:{
		expression for array
	},
 	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 = f();
l[i] = 900;
q.e = "this is a text";

AST

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

Example

Function definition

Language

fn function_name:return_type (parameter1:[parameter1_type][=value], ...) => expression;
fn function_name:return_type (parameter1:[parameter1_type][:=value], ...) 
begin
   ...
end;

Examples

fn f1:none ()
begin
end;
fn sum:integer (a:integer, b:integer) => a+b;
fn power:integer (a:integer, n:integer)
begin
  def p:integer;
  def i:integer;
  if n == 0 then 
  begin
     power=1;
  end;
  for i in [1,n] p=p * a;
  return p;
end;

AST

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

Example

Return

This is the statement that sets the return value of the function.

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

function call

The is a call for a function

Language

function_name (parameter_name1:value1, parameter_name2:value, ...);
function_name (parameter1_value, parameter2_value, ...)

Examples

write (text:"Text");
s = sum (3, 5);
getdir ();

AST

{
	id:"function_call",
	function:"function_name",
	parameters:{ // dictionary of parameter name or index and value
 		"parameter1": parameter_value,
 		"2":parameter value,
 		...
 	}
}

Example

Branch

Language

if expression then
  ... 
end;
if expression then
  ... 
else
  ...
end;

AST

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

Example

Loops

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

loop run

loop expression run
  ...
end;
AST
{
	id:"loop_run",
	exp :expression,
	statements: [ // array of statements
 
	]
}

loop ... when

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

for

for variable_name in expression run
   ...
end;

or

for variable_name from number to number run
   ...
end;
for variable_name from number downto number run
   ...
end;
AST
{
	id:"for",
        variable:variable_name,
	exp:{
		expression 
		...
	}
	statements: [ // array of statements
 
	]
}

or

{
	id:"for",
        variable:variable_name,
	from: { expression },
        to/downto: { expression },
	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}
#endif

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 l and y file (grammar.l and grammar.y)
    • 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.y grammar.l -o grammar.js
alf/teme/tema3_en_draft.txt · Last modified: 2019/04/03 10:25 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