Exploring Pre-Programmed Interaction with Expect in Linguistic Programming
In this lecture, we delve into the use of Expect, a programming tool for executing pre-programmed interactions. We start by reviewing the graded homework and then explore how to utilize Expect commands to automate interactions within the terminal. Key features include string matching and response generation using TCL (Tool Control Language). Through interactive examples, like a knock-knock joke responder, we demonstrate pattern matching with regular expressions and how to store matched patterns in variables, highlighting its applications in linguistic programming scenarios.
Exploring Pre-Programmed Interaction with Expect in Linguistic Programming
E N D
Presentation Transcript
LING 408/508: Programming for Linguists Lecture 7 September 16th
Administrivia • Homework 3 graded
Here document • Pre-programmed interaction: • (here document: inline file) rm () { /bin/rm -i "$@" } export -f rm #!/bin/bash bash confirm.sh $1 <<EOF y EOF #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 #!/bin/bash bash confirm.sh $1 <<<y
Interaction using expect • expect is a program that executes preprogrammed interaction using commands including: • expect string look for string in input • send string respond with string (/r for return) • spawn string execute a program under expect • interact gives control to user output will appear on terminal expect is written using a programming language called TCL (Tool Control Language) aka "tickle" #!/usr/bin/expect -f expect "hello" send "world\n"
Interaction using expect • Knock knock joke responder: • #!/usr/bin/expect -f • expect "knock knock\n" • send "who's there?\n" • expect -re "(.*)\n" • send "$expect_out(1,string) who?\n" • expect "\n" • send "Very funny\n" • [1934 Knock knock joke from Wikipedia]
Interaction using expect • expect can pattern match against regular expressions using flag –re • expect -re "(.*)\n" • matching patterns are stored in variables: • $expect_out(1,string)
Interaction using expect • Let's interact with the BMI homework solution …