[ragel-users] Keywords and actions in a minimal example

Adrian Thurston thurston at complang.org
Sat Nov 23 14:55:20 UTC 2013


Hi Lane,

The simplest way to do it is to exclude the keywords from the 
identifiers using set difference. You can use (vars - keys).

You can also prioritize actions appropriately (keywords referenced 
before idents) and break out of the action processing loop when you 
execute the higher priority action.

You can also use a scanner.

-Adrian

On 13-06-25 02:20 PM, Lane Schwartz wrote:
> I have a minimal example, where tokens are separated by spaces. In
> this example "a" is one allowed pattern, and alpha+ is the other.
>
> I would like to trigger one particular action (let's say it prints
> "keyword") when the pattern "a" has been recognized, and a different
> action when the alpha+ pattern has been recognized (let's say it
> prints "variable").
>
> I've worked through most of the samples in the tutorial, but I'm still
> stumped on this basic question:
>
> How can I trigger these actions such that the action that prints
> "keyword" is triggered for the input "a" but not for the input "abc",
> and the action that prints "variable" is triggered for the input "abc"
> but not for the input "a"?
>
> Given the input "a bc abc def a gh a", I would like to see the following output:
> keyword
> variable
> variable
> variable
> keyword
> variable
> keyword
>
> My exact example is below. This is my first time posting to this list.
> I appreciate any help or tips, and if this isn't the right place to
> post please let me know. FWIW, I've tried using key @keyword, key
> %keyword, key %*keyword, var @name, var %name, var %*name, and several
> other user action triggers from chapter 3 of the manual.
>
> #include<iostream>
> #include<sstream>
> #include<string.h>
> #include<stdio.h>
>
> void parse() {
>
>       char *p   = "a bc abc def a gh a";
>       char *pe  = p + strlen(p);
>       char *eof = p + strlen(p);
>       int cs;
>
>       %%{
>
> 	machine minimal;
> 	write data;
>
> 	action keyword {
> 	       std::cerr<<  "keyword"<<  std::endl;
> 	}
>
> 	action name {
> 	       std::cerr<<  "variable"<<  std::endl;
> 	}
>
> 	key = "a";
>
> 	var = alpha+;
>
> 	whitespace = ' '*;
>
> 	main := (( key | var ) whitespace )* ;
>
> 	write init;
> 	write exec;
>
>       }%%
>
> }
>
> int main() {
>      parse();
> }
>
>
> Thanks,
> Lane
>
> _______________________________________________
> ragel-users mailing list
> ragel-users at complang.org
> http://www.complang.org/mailman/listinfo/ragel-users

_______________________________________________
ragel-users mailing list
ragel-users at complang.org
http://www.complang.org/mailman/listinfo/ragel-users



More information about the ragel-users mailing list