|
|
 |
 |
 |
 |
TCL(Tool Command Language) Scripting
|
 |
 |
 |
 |
 |
 |
 |
 |
?? exec prog << value ??
Hello comp.lang.tcl !!! I have the following problem: I have created a program with with flex and bison (lex, yacc), a small calculator, now i want to execute the program within a tcl-script. Therefore I write exec prog << var where var shall be the mathematical expression, e.g. 2+2 Now I thought, the prog will be executed and by using << the value of var (string) 2+2 will be passed as input to prog. Unfortunately, there is only the message "syntax error" instead of - as i have expected - 4. Is anybody able to give me a hint? Thanks, Zeh Mau
Zeh Mau wrote: > Hello comp.lang.tcl !!! > I have the following problem: > I have created a program with with flex and bison (lex, yacc), > a small calculator, > now i want to execute the program within a tcl-script. > Therefore I write > exec prog << var > where var shall be the mathematical expression, e.g. 2+2 > Now I thought, > the prog will be executed and by using > << > the value of var (string) 2+2 will be passed as input to prog. > Unfortunately, there is only > the message "syntax error" instead of - as i have expected - 4.
Show us the complete and actual error message, as well as the exact line of code. Otherwise we can only guess. In addition, also tell us what happens when you do "echo '2+2' | prog" from the command line. -- Bryan Oakley http://www.tclscripting.com
Zeh Mau wrote: > exec prog << var > where var shall be the mathematical expression, e.g. 2+2
No, var will always be the literal string "var". If you want the value of the variable var you have to use $var, as in: exec prog << $var Schelte. -- set Reply-To [string map {nospam schelte} $header(From)]
On 31 Mai, 19:31, Bryan Oakley <oak@bardo.clearlight.com> wrote:
> Zeh Mau wrote: > > Hello comp.lang.tcl !!! > > I have the following problem: > > I have created a program with with flex and bison (lex, yacc), > > a small calculator, > > now i want to execute the program within a tcl-script. > > Therefore I write > > exec prog << var > > where var shall be the mathematical expression, e.g. 2+2 > > Now I thought, > > the prog will be executed and by using > > << > > the value of var (string) 2+2 will be passed as input to prog. > > Unfortunately, there is only > > the message "syntax error" instead of - as i have expected - 4. > Show us the complete and actual error message, as well as the exact line > of code. Otherwise we can only guess. > In addition, also tell us what happens when you do "echo '2+2' | prog" > from the command line. > -- > Bryan Oakleyhttp://www.tclscripting.com
In normal case, I type prog then press return then I type 2+2 and press return the result is shown in the next line. Whole procedure: prog -> return 2+2 -> return 4.0000 I expexted when I write exec prog this is the same situation as lcalc + return the << implies that 2+2 will be passed as input for which the prog is waiting then the result is calculated by the prog and should be stored in a new variable. Zeh Mau
In article <DDD7i.6794$C96.2@newssvr23.news.prodigy.net>, Bryan Oakley <oak@bardo.clearlight.com> wrote:
>Zeh Mau wrote: >> Hello comp.lang.tcl !!! >> I have the following problem: >> I have created a program with with flex and bison (lex, yacc), >> a small calculator, >> now i want to execute the program within a tcl-script. >> Therefore I write >> exec prog << var >> where var shall be the mathematical expression, e.g. 2+2 >> Now I thought, >> the prog will be executed and by using >> << >> the value of var (string) 2+2 will be passed as input to prog. >> Unfortunately, there is only >> the message "syntax error" instead of - as i have expected - 4. >Show us the complete and actual error message, as well as the exact line >of code. Otherwise we can only guess. >In addition, also tell us what happens when you do "echo '2+2' | prog" >from the command line.
. . . Possible variations that occur to me: exec prog << $var exec prog << $var\n I can easily imagine, though, that exec prog << var tosses an exception.
On 31 Mai, 20:49, cla@lairds.us (Cameron Laird) wrote:
> In article <DDD7i.6794$C96.2 @newssvr23.news.prodigy.net>, > Bryan Oakley <oak @bardo.clearlight.com> wrote: > >Zeh Mau wrote: > >> Hello comp.lang.tcl !!! > >> I have the following problem: > >> I have created a program with with flex and bison (lex, yacc), > >> a small calculator, > >> now i want to execute the program within a tcl-script. > >> Therefore I write > >> exec prog << var > >> where var shall be the mathematical expression, e.g. 2+2 > >> Now I thought, > >> the prog will be executed and by using > >> << > >> the value of var (string) 2+2 will be passed as input to prog. > >> Unfortunately, there is only > >> the message "syntax error" instead of - as i have expected - 4. > >Show us the complete and actual error message, as well as the exact line > >of code. Otherwise we can only guess. > >In addition, also tell us what happens when you do "echo '2+2' | prog" > >from the command line. > . > . > . > Possible variations that occur to me: > exec prog << $var > exec prog << $var\n > I can easily imagine, though, that > exec prog << var > tosses an exception.
Hello Cameron, you are right, I expected your second possibility also to be the right version, but the prog rises an error "syntax error", which means the expression of $var\n is not a mathematical expression like 2+2, so the prog refuses it. Perhaps I have a misunderstanding and it is not possible to start a program and give to him the information not over sdtin but by a variable. But, thanks a lot :)
Zeh Mau wrote: > On 31 Mai, 20:49, cla @lairds.us (Cameron Laird) wrote: >> In article <DDD7i.6794$C96.2 @newssvr23.news.prodigy.net>, >> Bryan Oakley <oak @bardo.clearlight.com> wrote: >>> Zeh Mau wrote: >>>> Hello comp.lang.tcl !!! >>>> I have the following problem: >>>> I have created a program with with flex and bison (lex, yacc), >>>> a small calculator, >>>> now i want to execute the program within a tcl-script. >>>> Therefore I write >>>> exec prog << var >>>> where var shall be the mathematical expression, e.g. 2+2 >>>> Now I thought, >>>> the prog will be executed and by using >>>> << >>>> the value of var (string) 2+2 will be passed as input to prog. >>>> Unfortunately, there is only >>>> the message "syntax error" instead of - as i have expected - 4. >>> Show us the complete and actual error message, as well as the exact line >>> of code. Otherwise we can only guess. >>> In addition, also tell us what happens when you do "echo '2+2' | prog" >> >from the command line. >> . >> . >> . >> Possible variations that occur to me: >> exec prog << $var >> exec prog << $var\n >> I can easily imagine, though, that >> exec prog << var >> tosses an exception. > Hello Cameron, > you are right, I expected your second possibility also to be the right > version, > but the prog rises an error "syntax error", > which means the expression of $var\n is not a mathematical expression > like 2+2, > so the prog refuses it. > Perhaps I have a misunderstanding and it is not possible to start a > program > and give to him the information not over sdtin but by a variable.
It is absolutely possible. Your code has a bug. If you want better help, give us better information. Show us the *exact* tcl code you are using and the *exact* error message and we might be able to help you more. Often, people make assumptions about their code which are false but we can't see the code so we must go on the description that is given to us. If you can show us the actual tcl code and actual error it might shed light on the problem. -- Bryan Oakley http://www.tclscripting.com
Ok, I have a file called example.tcl the file contains the following commands: ----------------------------------------- set var 4+4\n puts $var set result [exec test_prog << $var] puts $result ----------------------------------------- The output is: ----------------------------------------- 4+4 syntax error can't read "result": no such variable ----------------------------------------- The message "syntax error" is created by test_prog when there is no mathematical expression. Thanks for your help, Zeh Mau
Zeh Mau wrote: > Ok, I have a file called example.tcl > the file contains the following commands: > ----------------------------------------- > set var 4+4\n > puts $var > set result [exec test_prog << $var] > puts $result > ----------------------------------------- > The output is: > ----------------------------------------- > 4+4 > syntax error > can't read "result": no such variable > ----------------------------------------- > The message "syntax error" is created by > test_prog when there is no mathematical expression. > Thanks for your help, > Zeh Mau
Ok, your tcl code is correct. Your test_prog must have a bug. Can you run your test_prog from the command line, like the following? echo '4.4' | test_prog My guess is, if you run it from the command line you will get the same error. -- Bryan Oakley http://www.tclscripting.com
On May 31, 9:53 pm, Zeh Mau <chefmue@web.de> wrote: > set var 4+4\n > puts $var > set result [exec test_prog << $var] > puts $result > ----------------------------------------- > 4+4 > syntax error > can't read "result": no such variable
Very strange. Either test_prog exits with status 0 and result gets set to something (be it an empty string), or it exits abnormally, and [exec] raises an exception, hence cannot reach the [puts]. Can you please run strace (I mean "strace tclsh example.tcl") and post the output ? -Alex
On 31 Mai, 22:16, Alexandre Ferrieux <alexandre.ferri@gmail.com> wrote:
> On May 31, 9:53 pm, Zeh Mau <chefmue @web.de> wrote: > > set var 4+4\n > > puts $var > > set result [exec test_prog << $var] > > puts $result > > ----------------------------------------- > > 4+4 > > syntax error > > can't read "result": no such variable > Very strange. Either test_prog exits with status 0 and result gets set > to something (be it an empty string), or it exits abnormally, and > [exec] raises an exception, hence cannot reach the [puts]. > Can you please run strace (I mean "strace tclsh example.tcl") and post > the output ? > -Alex
You are right, the test_prog exits with exit(0), I also tried return(1). The error message now is: syntax error while executing "exec test_prog << $var" invoked from within "set result [exec test_prog << $var]" (file "example.tcl" line 5)
On May 31, 10:55 pm, Zeh Mau <chefmue@web.de> wrote: > You are right, the test_prog exits with exit(0), > I also tried return(1). > The error message now is:
Please be consistent. Don't change everything simultaneously. Freeze your test_prog to whatever behavior makes sense for you, and by all means describe one single situation. I'll assume your previous messages are simply an inconsistent description, until proven to the contrary. Let's see: It is customary in unix to do exit(0) in case of success and exit(value other than zero) otherwise. So, it is natural for your test_prog to exit(1) after saying "syntax error". Tcl transmits this information by raising an error from within [exec]. The second criterion leading to an error in [exec] is the child writing to stderr, unless you explicitly redirect is with 2> or 2>@ (this is for historical reasons, some old commands stubbornly exiting with 0 even in case of error). So, a good idiom is to systematically forward the child's stderr to tclsh's: set result [exec tst_prog << $var 2>@ stderr] this way, the *only* reason for an error in [exec] will be the exit status. After that, it's up to you to figure out whether the child is right to have this behavior. If this isn't enough to help you out, please follow my previous suggestion about strace. It is really the simplest and maybe only way for us to help you without having to dive into the source of your test_prog. -Alex
> syntax error > while executing > "exec test_prog << $var" > invoked from within > "set result [exec test_prog << $var]" > (file "example.tcl" line 5)- Hide quoted text - >
* Zeh Mau <chefmue@web.de> | I also tried return(1). | | The error message now is: | | syntax error | while executing | "exec test_prog << $var" | invoked from within | "set result [exec test_prog << $var]" | (file "example.tcl" line 5) Please try the suggestion of Bryan first: run echo "2+2"| test_prog from the command line, and see whether this also gives an error. If it does, this has nothing to do with TCL but where your program reads its input from and how. Which OS are you running this on? Which TCL version are you using? R'
|
 |
 |
 |
 |
|