Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Anyone try the tutorial? Perl –More about Regular expressions

3 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 In terms of regular expression repetition quantifiers, what does greedy mean? A quantifier is greedy if it matches as much of the string as possible while still allowing the whole regular expression to match. We'll see that greediness in action now. Let’s continue looking at this site for examples of matching repetitions and the 4 principles that are followed: http://www.cs.rit.edu/~afb/20013/plc/perl5/doc/perlretut.html

4 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Recap on the special variables we learned $_ $` $& and $' (left, match, right)‏ $0 (program name)‏ $1, $2, $3,... (the submatches)‏

5 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's write a few regular expressions. match any signed or unsigned integers of arbitrary length. e.g. it should match –-22 –4567 –1 –+43 but not things like: –- –+ –4.56 –abcd –etc.

6 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's try these: 1) ignore beginning whitespace if there is any, and match the word program and store the rest of the string (after the word program) into some variable. 2) Now what if there were \n's in the string? What might we change? 3) cs330 or cs106 or CS106 or CS330 but not Cs330, or cS106 etc.

7 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 1) ignore beginning whitespace if there is any, and match the word program and store the rest of the string (after the word program) into some variable. m/\s*program(.*)/ 2) Now what if there were \n's in the string? What might we change? m/\s*program(.*)/s 3) cs330 or cs106 or CS106 or CS330 but not Cs330, or cS106 etc. m/cs330|cs106|CS330|CS106/ OR m/(cs|CS)(106|330)/

8 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let’s look at a larger parsing example using many of the features we just learned. We'll read the problem and try to solve it ourselves before looking at the solution. The “doing string selections” section of: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm The following page is a good page for reference. It is a nice summary of the different characters and their meanings with succinct examples: http://www.cs.tut.fi/~jkorpela/perl/regexp.html

9 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Declaring and defining subroutines sub name { # code goes here… } Call subroutines &name; &name( parm1, parm2, parm3); # any number of parameters allowed separated by commas name(parm1, parm2, parm3); # the ampersand is optional if using parameters in parentheses.

10 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Parameters in subroutines are stored in a parameter array @_ The value returned from a subroutine is the value of the last evaluation done inside it. Be aware of this! Or you can explicitly use the return function to return a value and terminate the subroutine. Local variables in subroutines are declared by using the my keyword –When declaring more than one, you must put them in parentheses or else all but the first one will be considered global. –my ($parm1, $parm2); When calling the subroutine, the arguments passed in are put in the @_ special array for that subroutine.

11 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There are a multitude of ways to get at the contents of the elements in the parameter array @_ One of which is $_[0], $_[1], etc... Note well: These have NOTHING to do with the $_ special default variable which we have seen. Another way is to use shift. Shift will shift in the next array element into the variable on the LHS and remove that element from the array. e.g. my $filehand = shift; note: shift works for any array, not just the paramater array. e.g. shift @myarray Another way would be to do something like: my ($age, $height, $weight) = @_;

12 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Common code problems I saw last year. Not using chomp on the input (and then comparing the input variable to some string that doesn’t have a newline after it.)‏ Comparing strings using ==, !=, etc. instead of the correct way which is to use eq, ne, etc. When comparing non-numeric strings to numerics, the strings are evaluated as zero which trips people up. Solution would be to: Use chomp on the input variable Use the string comparison operators to compare strings (don’t use ==, !=,,etc. which should only be used to compare numeric data.)‏

13 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's start to implement a recursive descent parser for the language in the text book (p. 185) What are the features of a recursive decent parser again? Assuming the thing we're parsing is stored in a file we would like to read in the whole file in one string (including \n's). The reason the reading of a file reads one line at a time is because Perl has a special variable, $/, that holds what “termination character(s)” which in our case is usually set to be “\n”. We can temporarily store what the termination character(s) are, then undef the $/, then read the whole file into a scalar and then store the termination characters back into $/.

14 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There's so much more about Perl that I haven't gone over. You should now be able to learn more on your own. There are many functions I haven't gone over that are useful –Substring –Split –Etc. –Find out about the functions (with examples) at these 2 pages: –http://www.unix.org.ua/orelly/perl/prog3/ch29_01.htmhttp://www.unix.org.ua/orelly/perl/prog3/ch29_01.htm –http://www.unix.org.ua/orelly/perl/prog3/ch29_02.htm


Download ppt "CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google