11/6/2013 CSCI-2325: INSIDE PERL MOHAMMAD T. IRFAN 11/6/13 Origin • Scripting language based on sh and awk • Larry Wall: “People often argue over the difference between a scripting language and a programming language. If you ask me, I’ll tell you that a script is what you hand the actors, and a program is what you hand the audience.” • It is very much an imperative programming language • Always compiled before execution 1
11/6/2013 Variables • Implicitly declared • 3 namespaces for variables (with special start symbols for name) • Scalar: $ • Array: @ • Hash: % • Larry Wall’s explanation: It’s similar to natural languages • Implicit variables • Example: @_ Scalar Variables • May contain both numbers and strings • Depending on the context numbers are converted to strings and vice versa • Allows changing the value of a scalar variable to any type – number of string 2
11/6/2013 Arrays • Arrays are dynamic • Can have “empty” cells, which are denoted as “undefined” • Array indexing cannot be checked Scoping • Dynamic scoping: References to names are resolved dynamically • Allows my keyword to avoid unexpected effects • local keyword can be used for dynamic scoping 3
11/6/2013 Example: Dynamic Scoping $x = 5; $y = 10; myFunction(); # call subroutine – no argument print "x = $x, y = $y\n"; sub myFunction { local ($x, $y); #make it my ($x,$y) to swap global var $x = 100, $y = 200; swap(); # no argument print "After swap: x = $x, y = $y\n"; } sub swap { $t = $x; #Is this the x in myFunciton or the global x? $x = $y; $y = $t; } Type System • Review [Caution: there are different definitions of these terms] • A language is statically typed , if the types of all variables are fixed when they are declared at compile time • A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned • A language is strongly typed if its type system can detect all type errors either at compile time or at run time • “Weakly typed” is not well -defined – may not be related to strongly typed • Examples • C is statically typed, but not strongly typed (hint: pointer, array index) • Java is both statically and strongly typed • Perl is both dynamically and strongly typed 4
11/6/2013 Semantics • Expression, assignment – similar to C • Copy semantics • Iterative statements • A variety of for , while , and do-while statements • Larry Wall’s philosophy: Often, there are many ways of expressing the same idea using natural languages • Conditional statements • if-elsif-else statements, other types of if and unless statements • Subroutine • Parameters are passed implicitly: array @_ • Can return multiple items using an array • Always returns the last computed value, even if there is no return statement • Object-oriented features • package • Exception handling Applications • System administration • Text processing • Computational biology and AI 5
11/6/2013 BUILDING A LEXICAL ANALYZER FOR CLITE Input File void main() { int i; double jVar2; jVar2 = 10 + i + 50.99; } 6
11/6/2013 Desired Output Type void main main ( ( ) ) { { Type int Identifier i ; ; Type double Identifier jVar2 ; ; Identifier jVar2 = = IntegerLiteral 10 + + Identifier i + + DoubleLiteral 50.99 ; ; } } How to run perl with arguments? • perl lexer.pl source.clite • You may enter it in Padre: Run Run Command 7
11/6/2013 lexer.pl: A first attempt Open input and output files Read each line from the file Process that line 8
11/6/2013 • Subroutine to print a token-lexeme pair to the output file • Used by the match() subroutine 9
11/6/2013 10
11/6/2013 A MORE ELEGANT WAY 11
11/6/2013 12
Recommend
More recommend