CS530
Date: 9/19
Overview:
1) Discussed Homework
2) Mini-Language Core – Things to note
3) FORTRAN Programming Assignment – Interesting Observations
4) Chapter 5
5) Pascal
6) TODO
Discussed homework
- common mistakes
- performing two steps at a time when doing leftmost derivations
- only make one change during a single step
- producing two distinct parse trees means there is ambiguity
- Solution to exercise (8):
<S> / <A> /
| \
<A> + <A>
/ | \ |
<A> + <A> <id> | | |
<id> <id> c
| | a b
<S>
/
<A>
/ | \
<A> + <A>
| / | \
<id> <A> + <A>
| | |
a <id> <id>
| |
b c
Mini-Language Core –
Things to note
Remember:
- keyword must be lowercase
- declarations go before begin
- variable names must be uppercase (only uppercase letters)
- if statement and while statement comparison must have parentheses
- if statement requires then and end if;
- could only output variable name (not a string)
- can’t declare a number
- assignment operator is a colon AND equal (:=)
- operator made up of an expression needs parentheses
- never says that all variables have to be declared
- no problem with nested statements
- can’t leave angle brackets on entities
- increment operator does not exist (can’t say a++)
- statements require semi-colons at end
- declaration is not the same as an assignment statement
-
while statement requires loop and end loop;
Example of a valid program:
program
A : integer;
begin
A := 5;
output A;
end;
Example
of a simple loop:
while (x > 0) loop
x := x + 1;
end loop;
Grammers can be used to do two things:
1) generate a string
2) parse a string or sentence – determine if it is valid
Questions:
1) In mini-language core, how can you calculate a quotient?
- repeatedly subtract using a loop
2) Can there be a remainder? No, because there are no floats.
FORTRAN Programming
Assignment – Interesting Observations
There are two options when creating subroutines:
1) can place them before main unit (don’t do it this way!)
2) can place them after the main unit
A = 3.5
B = 7.9
C output A & B
CALL swap(A,B)
C output A & B
stop
end
SUBROUTINE swap(C,D)
Temp = D
C = D
D = Temp
return
end
Be
careful:
- variables passed to a subroutine or function should be of the same type; if not, they must be redeclared (formal parameters must match actual parameters)
- if parameter types don’t match, program seems to work if subroutines follow the main unit
- if parameter types don’t match and subroutines appear before main unit, then truncation will occur
- why? subroutine and variable declarations are bound before the main unit and this causes problems (not particularly clear yet why)
Discussion:
- there is a difference in the way the symbol table is built and how the associations between variables work
Guidelines:
1) subroutines and functions should follow main unit
2) formal parameters should match actual parameters
Common Problems:
- documentation not suitable
-
using if then, end if
- when passing arrays to subroutines, you pass the array itself and the size and re-dimension it inside the subroutine
- you have to tell the subroutine you’re giving it an array; it’s a reminder of the array’s size and is needed for referencing individual elements within the array (element indices have to be computed based on a base address and size of array elements)
- should always have a “program ended” or something similar when your program ends
Chapter 5
- should have read chapter 5
Pascal
- is very precise and must be followed strictly
- alternative download:
Free Pascal 2.2
www.download.com
- README.txt
- testing the compiler
- change to the demo directory and type:
ppc386 hello
hello
- every Pascal program:
- starts with a reserved word: program
- if you leave it out, some compilers will let you get away with it---don’t leave it out!
- has a: begin
-
ends with a: end.
- variable declarations start with: var
program <identifier> (input, output);
var <identifier> : <data_type>;
begin
end.
- program is a reserved word
- <indentifier> is the name of your program
Other notes:
- when making changes to code, it’s helpful to save it as a different file (append a digit to the end)
- in the future, you’ll probably be asked to hand in an output file in addition to your programming assignment code
TODO:
- Chapter 5 problems:
9, 10