SlideShare a Scribd company logo
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Language processing
Course "Software Language Engineering"
University of Koblenz-Landau
Department of Computer Science
Ralf Lämmel
Software Languages Team
1
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Summary
2
This is an introduction to language processing.
We classify components of a language definition.
We classify components in language processing.
We illustrate language processing for a simple language.
The processing components are written in Haskell.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Language definition components
Concrete syntax (useful for parsing and humans)
Abstract syntax (useful for most processing)
Type system
Extra rules
Dynamic semantics
Translation semantics
Pragmatics
3
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Language processing components
Recognizer (execute concrete syntax)
Parser (return parse/syntax trees)
Option 1: Return concrete parse trees
Option 2: Return abstract syntax trees
Imploder (parse tree to abstract syntax trees)
Pretty printer or unparser (return text again)
4
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Language processing components
cont’d
Type checker
Interpreter
Compiler to machine language
Translator / generator to high-level language
Software visualizers (e.g., call graph or flow chart)
Software analyzers (e.g., dead-code detection)
Software transformers (e.g., dead-code elimination)
Software metrics tools (e.g., cyclomatic complexity)
IDE integration (coloring, navigation, ...)
5
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
How to do language processors?
What programming techniques to use?
What programming technologies to use?
Code generators
APIs / combinator libraries
Metaprogramming frameworks
How to leverage language definitions?
6
Let’s get somedata points today.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Haskell-based language processors
for a simple imperative language
7
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
A Pico example
8
begin declare input : natural,
              output : natural,
              repnr : natural,
              rep : natural;
      input := 14;
      output := 1;
      while input - 1 do
          rep := output;
          repnr := input;
          while repnr - 1 do
             output := output + rep;
             repnr := repnr - 1
          od;
          input := input - 1
      od
end
Factorial function in
the simple imperative
language Pico
Pico is an illustrative language which has been used by the SEN1/SWAT team at CWI Amsterdam for many years.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Components for Pico
9
Abstract syntax
Recognizer
Parser
Type checker
Interpreter
Pretty printer
Assembly code
Compiler
Machine
Flow charts
Visualizer
https://github.com/slecourse/slecourse/tree/master/sources/pico/
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Abstract syntax
10
type Name = String
data Type = NatType | StrType
type Program = ([Decl], [Stm])
type Decl = (Name,Type)
data Stm
= Assign Name Expr
| IfStm Expr [Stm] [Stm]
| While Expr [Stm]
data Expr
= Id Name
| NatCon Int
| StrCon String
| Add Expr Expr
| Sub Expr Expr
| Conc Expr Expr
+ implementationaldetails (“deriving”)
See Haskell code online: AbstractSyntax.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Concrete syntax to be recognized
11
Program ="begin" Declarations {Statement ";"} "end" ;
Declarations = "declare" {Declaration ","}* ";" ;
Declaration = Id ":" Type;
Type = "natural" | "string" ;
Statement
= Id ":=" Expression
| "if" Expression "then" {Statement ";"}* "else" {Statement ";"}* "fi"
| "while" Expression "do" {Statement ";"}* "od"
;
Expression
= Id
| String
| Natural
| "(" Expression ")"
| Expression "+" Expression
| Expression "-" Expression
| Expression "||" Expression
Id = [a-z][a-z0-9]* !>> [a-z0-9];
Natural = [0-9]+ ;
String = """ !["]* """;
We need to
implement this
grammar.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Syntax vs. recognizer
Syntax definition may be declarative / technology-independent.
Unambiguous grammar needed for recognition.
Think of dangling else or operator priorities.
Recognizers may be hand-written.
Recognizers specs may be non-declarative / technology-dependent.
12
The same is
true for parsers.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Recognizer
Leverage parser combinators (Parsec)
Recognizer = functional program.
Parser combinators are monadic.
13
See Haskell code online: Recognizer.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Parsec
(http://www.haskell.org/haskellwiki/Parsec, 23 April 2013)
“Parsec is an industrial strength, monadic parser combinator library for Haskell. It can parse
context-sensitive, infinite look-ahead grammars but it performs best on predictive (LL[1])
grammars.”
14
See also:
http://101companies.org/wiki/Technology:Parsec
http://hackage.haskell.org/packages/archive/parsec/3.1.3/doc/html/Text-Parsec.html
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Parser
Add synthesis of abstract syntax terms to recognizer.
Concrete and abstract syntaxes are coupled up.
15
See Haskell code online: Parser.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Type checker
Check that ...
declarations are unambiguous;
all referenced variables are declared;
all expected operand types agree with actual ones;
...
16
See Haskell code online: TypeChecker.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Type system vs. checker
17
Type systems ...
are based on formal/declarative specifications;
they are possibly executable (perhaps inefficiently).
Type checkers ...
implement type systems efficiently;
they provide useful error messages.
See Haskell code online: TypeChecker.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Interpreter
Leverage natural semantics.
Define it as a total function.
Use a special error result.
Error messages could also be provided that way.
18
See Haskell code online: Interpreter.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Pretty printer
Map abstract syntax to text.
Composable documents.
Vertical / horizontal composition.
Indentation.
Another case of a combinator library (= DSL).
19
See Haskell code online: PrettyPrinter.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Assembly code
Stack-based assembly language.
Explicit notion of label and gotos.
Could be translated to Bytecode or x86 or ....
20
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Assembly code
21
type Label = String
type Name = String
data Instr
 = DclInt Name -- Reserve a memory location for an integer variable
 | DclStr Name -- Reserve a memory location for a string variable
 | PushNat Int -- Push integer constant on the stack
 | PushStr String -- Push string constant on the stack
 | Rvalue Name -- Push the value of a variable on the stack
 | Lvalue Name -- Push the address of a variable on the stack
 | AssignOp -- Assign value on top, to variable at address top-1
 | AddOp -- Replace top two stack values by their sum
 | SubOp -- Replace top two stack values by their difference
 | ConcOp -- Replace top two stack values by their concatenation
 | Label Label -- Associate a label with the next instruction
 | Go Label -- Go to instruction with given label
 | GoZero Label -- Go to instruction with given label, if top equals 0
 | GoNonZero Label -- Go to instruction with given label, if top not equal to 0
     deriving (Eq, Show, Read)
See Haskell code online: AssemblyCode.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Compiler
Use state to keep track of label generator.
Generate machine instructions compositionally.
A stream could also be used for linear output.
22
See Haskell code online: Compiler.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Machine
Very much like the interpreter.
Interpret the assembly code instructions.
Maintain a “memory” abstraction.
Maintain an “instruction” pointer.
23
See Haskell code online: Machine.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Flow charts
24
begin declare input : natural,
              output : natural,
              repnr : natural,
              rep : natural;
      input := 14;
      output := 1;
      while input - 1 do
          rep := output;
          repnr := input;
          while repnr - 1 do
             output := output + rep;
             repnr := repnr - 1
          od;
          input := input - 1
      od
end
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Flow charts
Define an abstract syntax of flow charts.
No concrete syntax needed.
Abstract syntax is mapped to “dot” (graphviz).
25
See Haskell code online: FlowChart.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Flow charts
26
See Haskell code online.
type FlowChart = ([Box], [Arrow])
type Box = (Id, BoxType)
type Id = String -- Identifier for boxes
data BoxType
 = Start
 | End
 | Decision Text
 | Activity Text
type Text = String -- Text to show
type Arrow = ((Id, FromType), Id)
data FromType
 = FromStart
 | FromActivity
 | FromYes
 | FromNo
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
“Dot” sublanguage for flowcharts
27
digraph FlowChart {
id1 [label="Start", shape=box, style=bold];
id2 [label="End", shape=box, style=bold];
id3 [label=" input := 14 ", shape=box];
id4 [label=" output := 1 ", shape=box];
id5 [label=" input - 1 ", shape=diamond];
id6 [label=" rep := output ", shape=box];
id7 [label=" repnr := input ", shape=box];
id8 [label=" repnr - 1 ", shape=diamond];
id9 [label=" output := output + rep ", shape=box];
id10 [label=" repnr := repnr - 1 ", shape=box];
id1 -> id3 [label=" ", headport= n , tailport= s ]
id3 -> id4 [label=" ", headport= n , tailport= s ]
id4 -> id5 [label=" ", headport= n , tailport= s ]
id5 -> id6 [label=" Yes ", headport= n , tailport= sw ]
id6 -> id7 [label=" ", headport= n , tailport= s ]
id7 -> id8 [label=" ", headport= n , tailport= s ]
id8 -> id9 [label=" Yes ", headport= n , tailport= sw ]
id9 -> id10 [label=" ", headport= n , tailport= s ]
id10 -> id7 [label=" ", headport= n , tailport= s ]
id8 -> id4 [label=" No ", headport= n , tailport= se ]
id5 -> id2 [label=" No ", headport= n , tailport= se ]
}
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
All of “dot”
28
dot User’s Manual, January 26, 2006 34
A Graph File Grammar
The following is an abstract grammar for the DOT language. Terminals are shown
in bold font and nonterminals in italics. Literal characters are given in single
quotes. Parentheses ( and ) indicate grouping when needed. Square brackets [
and ] enclose optional items. Vertical bars | separate alternatives.
graph → [strict] (digraph | graph) id ’{’ stmt-list ’}’
stmt-list → [stmt [’;’] [stmt-list ] ]
stmt → attr-stmt | node-stmt | edge-stmt | subgraph | id ’=’ id
attr-stmt → (graph | node | edge) attr-list
attr-list → ’[’ [a-list ] ’]’ [attr-list]
a-list → id ’=’ id [’,’] [a-list]
node-stmt → node-id [attr-list]
node-id → id [port]
port → port-location [port-angle] | port-angle [port-location]
port-location → ’:’ id | ’:’ ’(’ id ’,’ id ’)’
port-angle → ’@’ id
edge-stmt → (node-id | subgraph) edgeRHS [attr-list]
edgeRHS → edgeop (node-id | subgraph) [edgeRHS]
subgraph → [subgraph id] ’{’ stmt-list ’}’ | subgraph id
An id is any alphanumeric string not beginning with a digit, but possibly in-
cluding underscores; or a number; or any quoted string possibly containing escaped
quotes.
An edgeop is -> in directed graphs and -- in undirected graphs.
The language supports C++-style comments: /* */ and //.
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Visualizer
Very much like the compiler.
Generate low-level graph representation.
Also very much like the pretty printer.
Pretty print expressions and statement in boxes.
29
See Haskell code online: Visualizer.hs
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Concluding remarks
30
© 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle
Issues with Haskell study
31
Locations missing
Non-declarative implosion
Handling of priorities
More analyses of interest
Data-flow analysis
Control-flow analysis
IDE integration needed
Some of these issues are addressed
by a Rascal implementation of Pico.
http://www.rascal-mpl.org/

More Related Content

What's hot

Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
Sarit Chakraborty
 
Yacc
YaccYacc
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
codereplugd
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
Hamed Hatami
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
Taha Malampatti
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 
Compilation
CompilationCompilation
Compilation
David Halliday
 
Source-to-Source Compiler
Source-to-Source CompilerSource-to-Source Compiler
Source-to-Source Compiler
Mintoo Jakhmola
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
omercomail
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
Hossain Md Shakhawat
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
nTier Custom Solutions
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
BBDITM LUCKNOW
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
Munni28
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
JosephAlex21
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof Chethan Raj C
 
Nug2004 yhe
Nug2004 yheNug2004 yhe
Nug2004 yhe
Yassine Rafrafi
 
Web Programming UNIT VIII notes
Web Programming UNIT VIII notesWeb Programming UNIT VIII notes
Web Programming UNIT VIII notes
Bhavsingh Maloth
 

What's hot (20)

Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
 
Yacc
YaccYacc
Yacc
 
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 
Compilation
CompilationCompilation
Compilation
 
Source-to-Source Compiler
Source-to-Source CompilerSource-to-Source Compiler
Source-to-Source Compiler
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
 
Nug2004 yhe
Nug2004 yheNug2004 yhe
Nug2004 yhe
 
Web Programming UNIT VIII notes
Web Programming UNIT VIII notesWeb Programming UNIT VIII notes
Web Programming UNIT VIII notes
 

Similar to An introduction on language processing

Mastering Regex in Perl
Mastering Regex in PerlMastering Regex in Perl
Mastering Regex in Perl
Edureka!
 
An Introduction to Hamler
An Introduction to HamlerAn Introduction to Hamler
An Introduction to Hamler
EMQ
 
How does intellisense work?
How does intellisense work?How does intellisense work?
How does intellisense work?
Adam Friedman
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
megakott
 
Compiler lec 1
Compiler lec 1Compiler lec 1
Compiler lec 1
Ramadan Babers, PhD
 
Perl Reference.ppt
Perl Reference.pptPerl Reference.ppt
Perl Reference.ppt
AshleshaKulkarni4
 
Fundamentals of programming
Fundamentals of programmingFundamentals of programming
Fundamentals of programming
Kaycee Parcon
 
The Whole Platform A Language Workbench for Eclipse
The Whole Platform A Language Workbench for EclipseThe Whole Platform A Language Workbench for Eclipse
The Whole Platform A Language Workbench for Eclipse
Riccardo Solmi
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
wigewej294
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphi
Max Kleiner
 
Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementation
Bilal Maqbool ツ
 
Lecture1 compilers
Lecture1 compilersLecture1 compilers
Lecture1 compilers
Aftab Ahmad
 
Xtend api and_dsl_design_patterns_eclipse_confrance2016
Xtend api and_dsl_design_patterns_eclipse_confrance2016Xtend api and_dsl_design_patterns_eclipse_confrance2016
Xtend api and_dsl_design_patterns_eclipse_confrance2016
Max Bureck
 
Introduction To Computer Programming
Introduction To Computer ProgrammingIntroduction To Computer Programming
Introduction To Computer Programming
Hussain Buksh
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
jamesmarken1
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
AkarTaher
 
2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf
cifoxo
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net
Nico Ludwig
 
F# for Scala developers
F# for Scala developersF# for Scala developers
F# for Scala developers
Alfonso Garcia-Caro
 
Basics PHP
Basics PHPBasics PHP

Similar to An introduction on language processing (20)

Mastering Regex in Perl
Mastering Regex in PerlMastering Regex in Perl
Mastering Regex in Perl
 
An Introduction to Hamler
An Introduction to HamlerAn Introduction to Hamler
An Introduction to Hamler
 
How does intellisense work?
How does intellisense work?How does intellisense work?
How does intellisense work?
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
Compiler lec 1
Compiler lec 1Compiler lec 1
Compiler lec 1
 
Perl Reference.ppt
Perl Reference.pptPerl Reference.ppt
Perl Reference.ppt
 
Fundamentals of programming
Fundamentals of programmingFundamentals of programming
Fundamentals of programming
 
The Whole Platform A Language Workbench for Eclipse
The Whole Platform A Language Workbench for EclipseThe Whole Platform A Language Workbench for Eclipse
The Whole Platform A Language Workbench for Eclipse
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphi
 
Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementation
 
Lecture1 compilers
Lecture1 compilersLecture1 compilers
Lecture1 compilers
 
Xtend api and_dsl_design_patterns_eclipse_confrance2016
Xtend api and_dsl_design_patterns_eclipse_confrance2016Xtend api and_dsl_design_patterns_eclipse_confrance2016
Xtend api and_dsl_design_patterns_eclipse_confrance2016
 
Introduction To Computer Programming
Introduction To Computer ProgrammingIntroduction To Computer Programming
Introduction To Computer Programming
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net
 
F# for Scala developers
F# for Scala developersF# for Scala developers
F# for Scala developers
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 

More from Ralf Laemmel

Keynote at-icpc-2020
Keynote at-icpc-2020Keynote at-icpc-2020
Keynote at-icpc-2020
Ralf Laemmel
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structures
Ralf Laemmel
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scale
Ralf Laemmel
 
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Ralf Laemmel
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)
Ralf Laemmel
 
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Ralf Laemmel
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
Ralf Laemmel
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
Ralf Laemmel
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
Ralf Laemmel
 
Language processing patterns
Language processing patternsLanguage processing patterns
Language processing patterns
Ralf Laemmel
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)
Ralf Laemmel
 
XML data binding
XML data bindingXML data binding
XML data binding
Ralf Laemmel
 
Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)
Ralf Laemmel
 

More from Ralf Laemmel (14)

Keynote at-icpc-2020
Keynote at-icpc-2020Keynote at-icpc-2020
Keynote at-icpc-2020
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structures
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scale
 
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)
 
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
 
Language processing patterns
Language processing patternsLanguage processing patterns
Language processing patterns
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)
 
XML data binding
XML data bindingXML data binding
XML data binding
 
Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)
 

Recently uploaded

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
maazsz111
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
marufrahmanstratejm
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 

Recently uploaded (20)

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 

An introduction on language processing

  • 1. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Language processing Course "Software Language Engineering" University of Koblenz-Landau Department of Computer Science Ralf Lämmel Software Languages Team 1
  • 2. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Summary 2 This is an introduction to language processing. We classify components of a language definition. We classify components in language processing. We illustrate language processing for a simple language. The processing components are written in Haskell.
  • 3. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Language definition components Concrete syntax (useful for parsing and humans) Abstract syntax (useful for most processing) Type system Extra rules Dynamic semantics Translation semantics Pragmatics 3
  • 4. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Language processing components Recognizer (execute concrete syntax) Parser (return parse/syntax trees) Option 1: Return concrete parse trees Option 2: Return abstract syntax trees Imploder (parse tree to abstract syntax trees) Pretty printer or unparser (return text again) 4
  • 5. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Language processing components cont’d Type checker Interpreter Compiler to machine language Translator / generator to high-level language Software visualizers (e.g., call graph or flow chart) Software analyzers (e.g., dead-code detection) Software transformers (e.g., dead-code elimination) Software metrics tools (e.g., cyclomatic complexity) IDE integration (coloring, navigation, ...) 5
  • 6. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle How to do language processors? What programming techniques to use? What programming technologies to use? Code generators APIs / combinator libraries Metaprogramming frameworks How to leverage language definitions? 6 Let’s get somedata points today.
  • 7. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Haskell-based language processors for a simple imperative language 7
  • 8. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle A Pico example 8 begin declare input : natural,               output : natural,               repnr : natural,               rep : natural;       input := 14;       output := 1;       while input - 1 do           rep := output;           repnr := input;           while repnr - 1 do              output := output + rep;              repnr := repnr - 1           od;           input := input - 1       od end Factorial function in the simple imperative language Pico Pico is an illustrative language which has been used by the SEN1/SWAT team at CWI Amsterdam for many years.
  • 9. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Components for Pico 9 Abstract syntax Recognizer Parser Type checker Interpreter Pretty printer Assembly code Compiler Machine Flow charts Visualizer https://github.com/slecourse/slecourse/tree/master/sources/pico/
  • 10. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Abstract syntax 10 type Name = String data Type = NatType | StrType type Program = ([Decl], [Stm]) type Decl = (Name,Type) data Stm = Assign Name Expr | IfStm Expr [Stm] [Stm] | While Expr [Stm] data Expr = Id Name | NatCon Int | StrCon String | Add Expr Expr | Sub Expr Expr | Conc Expr Expr + implementationaldetails (“deriving”) See Haskell code online: AbstractSyntax.hs
  • 11. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Concrete syntax to be recognized 11 Program ="begin" Declarations {Statement ";"} "end" ; Declarations = "declare" {Declaration ","}* ";" ; Declaration = Id ":" Type; Type = "natural" | "string" ; Statement = Id ":=" Expression | "if" Expression "then" {Statement ";"}* "else" {Statement ";"}* "fi" | "while" Expression "do" {Statement ";"}* "od" ; Expression = Id | String | Natural | "(" Expression ")" | Expression "+" Expression | Expression "-" Expression | Expression "||" Expression Id = [a-z][a-z0-9]* !>> [a-z0-9]; Natural = [0-9]+ ; String = """ !["]* """; We need to implement this grammar.
  • 12. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Syntax vs. recognizer Syntax definition may be declarative / technology-independent. Unambiguous grammar needed for recognition. Think of dangling else or operator priorities. Recognizers may be hand-written. Recognizers specs may be non-declarative / technology-dependent. 12 The same is true for parsers.
  • 13. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Recognizer Leverage parser combinators (Parsec) Recognizer = functional program. Parser combinators are monadic. 13 See Haskell code online: Recognizer.hs
  • 14. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Parsec (http://www.haskell.org/haskellwiki/Parsec, 23 April 2013) “Parsec is an industrial strength, monadic parser combinator library for Haskell. It can parse context-sensitive, infinite look-ahead grammars but it performs best on predictive (LL[1]) grammars.” 14 See also: http://101companies.org/wiki/Technology:Parsec http://hackage.haskell.org/packages/archive/parsec/3.1.3/doc/html/Text-Parsec.html
  • 15. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Parser Add synthesis of abstract syntax terms to recognizer. Concrete and abstract syntaxes are coupled up. 15 See Haskell code online: Parser.hs
  • 16. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Type checker Check that ... declarations are unambiguous; all referenced variables are declared; all expected operand types agree with actual ones; ... 16 See Haskell code online: TypeChecker.hs
  • 17. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Type system vs. checker 17 Type systems ... are based on formal/declarative specifications; they are possibly executable (perhaps inefficiently). Type checkers ... implement type systems efficiently; they provide useful error messages. See Haskell code online: TypeChecker.hs
  • 18. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Interpreter Leverage natural semantics. Define it as a total function. Use a special error result. Error messages could also be provided that way. 18 See Haskell code online: Interpreter.hs
  • 19. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Pretty printer Map abstract syntax to text. Composable documents. Vertical / horizontal composition. Indentation. Another case of a combinator library (= DSL). 19 See Haskell code online: PrettyPrinter.hs
  • 20. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Assembly code Stack-based assembly language. Explicit notion of label and gotos. Could be translated to Bytecode or x86 or .... 20
  • 21. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Assembly code 21 type Label = String type Name = String data Instr  = DclInt Name -- Reserve a memory location for an integer variable  | DclStr Name -- Reserve a memory location for a string variable  | PushNat Int -- Push integer constant on the stack  | PushStr String -- Push string constant on the stack  | Rvalue Name -- Push the value of a variable on the stack  | Lvalue Name -- Push the address of a variable on the stack  | AssignOp -- Assign value on top, to variable at address top-1  | AddOp -- Replace top two stack values by their sum  | SubOp -- Replace top two stack values by their difference  | ConcOp -- Replace top two stack values by their concatenation  | Label Label -- Associate a label with the next instruction  | Go Label -- Go to instruction with given label  | GoZero Label -- Go to instruction with given label, if top equals 0  | GoNonZero Label -- Go to instruction with given label, if top not equal to 0      deriving (Eq, Show, Read) See Haskell code online: AssemblyCode.hs
  • 22. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Compiler Use state to keep track of label generator. Generate machine instructions compositionally. A stream could also be used for linear output. 22 See Haskell code online: Compiler.hs
  • 23. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Machine Very much like the interpreter. Interpret the assembly code instructions. Maintain a “memory” abstraction. Maintain an “instruction” pointer. 23 See Haskell code online: Machine.hs
  • 24. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Flow charts 24 begin declare input : natural,               output : natural,               repnr : natural,               rep : natural;       input := 14;       output := 1;       while input - 1 do           rep := output;           repnr := input;           while repnr - 1 do              output := output + rep;              repnr := repnr - 1           od;           input := input - 1       od end
  • 25. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Flow charts Define an abstract syntax of flow charts. No concrete syntax needed. Abstract syntax is mapped to “dot” (graphviz). 25 See Haskell code online: FlowChart.hs
  • 26. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Flow charts 26 See Haskell code online. type FlowChart = ([Box], [Arrow]) type Box = (Id, BoxType) type Id = String -- Identifier for boxes data BoxType  = Start  | End  | Decision Text  | Activity Text type Text = String -- Text to show type Arrow = ((Id, FromType), Id) data FromType  = FromStart  | FromActivity  | FromYes  | FromNo
  • 27. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle “Dot” sublanguage for flowcharts 27 digraph FlowChart { id1 [label="Start", shape=box, style=bold]; id2 [label="End", shape=box, style=bold]; id3 [label=" input := 14 ", shape=box]; id4 [label=" output := 1 ", shape=box]; id5 [label=" input - 1 ", shape=diamond]; id6 [label=" rep := output ", shape=box]; id7 [label=" repnr := input ", shape=box]; id8 [label=" repnr - 1 ", shape=diamond]; id9 [label=" output := output + rep ", shape=box]; id10 [label=" repnr := repnr - 1 ", shape=box]; id1 -> id3 [label=" ", headport= n , tailport= s ] id3 -> id4 [label=" ", headport= n , tailport= s ] id4 -> id5 [label=" ", headport= n , tailport= s ] id5 -> id6 [label=" Yes ", headport= n , tailport= sw ] id6 -> id7 [label=" ", headport= n , tailport= s ] id7 -> id8 [label=" ", headport= n , tailport= s ] id8 -> id9 [label=" Yes ", headport= n , tailport= sw ] id9 -> id10 [label=" ", headport= n , tailport= s ] id10 -> id7 [label=" ", headport= n , tailport= s ] id8 -> id4 [label=" No ", headport= n , tailport= se ] id5 -> id2 [label=" No ", headport= n , tailport= se ] }
  • 28. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle All of “dot” 28 dot User’s Manual, January 26, 2006 34 A Graph File Grammar The following is an abstract grammar for the DOT language. Terminals are shown in bold font and nonterminals in italics. Literal characters are given in single quotes. Parentheses ( and ) indicate grouping when needed. Square brackets [ and ] enclose optional items. Vertical bars | separate alternatives. graph → [strict] (digraph | graph) id ’{’ stmt-list ’}’ stmt-list → [stmt [’;’] [stmt-list ] ] stmt → attr-stmt | node-stmt | edge-stmt | subgraph | id ’=’ id attr-stmt → (graph | node | edge) attr-list attr-list → ’[’ [a-list ] ’]’ [attr-list] a-list → id ’=’ id [’,’] [a-list] node-stmt → node-id [attr-list] node-id → id [port] port → port-location [port-angle] | port-angle [port-location] port-location → ’:’ id | ’:’ ’(’ id ’,’ id ’)’ port-angle → ’@’ id edge-stmt → (node-id | subgraph) edgeRHS [attr-list] edgeRHS → edgeop (node-id | subgraph) [edgeRHS] subgraph → [subgraph id] ’{’ stmt-list ’}’ | subgraph id An id is any alphanumeric string not beginning with a digit, but possibly in- cluding underscores; or a number; or any quoted string possibly containing escaped quotes. An edgeop is -> in directed graphs and -- in undirected graphs. The language supports C++-style comments: /* */ and //.
  • 29. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Visualizer Very much like the compiler. Generate low-level graph representation. Also very much like the pretty printer. Pretty print expressions and statement in boxes. 29 See Haskell code online: Visualizer.hs
  • 30. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Concluding remarks 30
  • 31. © 2012-13 Ralf Lämmel, Software Language Engineering http://softlang.wikidot.com/course:sle Issues with Haskell study 31 Locations missing Non-declarative implosion Handling of priorities More analyses of interest Data-flow analysis Control-flow analysis IDE integration needed Some of these issues are addressed by a Rascal implementation of Pico. http://www.rascal-mpl.org/