location tracking

昨日謎に思った、bisonでの、文法規則部にあらわれた、謎の @1 と言う変数。

bison reference manualで調べた
http://www.gnu.org/software/bison/manual/html_mono/bison.html#Location-Tracking-Calc

これによると、location-tracking、すなわち、
構文解析の対象となっているテキストのどこに、
いまあつかっている情報が存在するか、の情報を格納しているオブジェクトみたいだ。


マニュアルでは、locationは

 typedef struct YYLTYPE
     {
       int first_line;
       int first_column;
       int last_line;
       int last_column;
     } YYLTYPE;

のような構造体で表されているみたいだけど、


%skeleton "lalr1.cc"としているからか、
手元で生成したコードでは

class location {
 ...
 
 std::ostream& operator<< (std::ostream& ostr, const location& loc)
 {
   ...
 }
};

とクラスになっていた。



operator<<がオーバロードされていたので、実際に値を見てみると
確かに、解析されているテキストでの位置情報がわたされてるっぽい。



で、このlocation-tracking、何に使うのかというとエラー表示などに有用らしい。


が、
Remember that computing locations is not a matter of syntax.
と書いてあるように、構文解析に必須の変数ではないらしい。